Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
You can of course use C-style arrays in Objective-C. I do that with constant arrays, where I build them at compile time and don't modify the contents, similar to the example you showed. You can put constant pointers to objects in them, of which the most likely, or maybe only, kind is NSString*
I also sometimes will use an NSArray or NSDictionary for the same purpose. I'll have an accessor that builds the NSArray/NSDictionary lazily. These objects have some advantages over C-style arrays.
Pitfall here again... Armed with the comment that it should work I made some progress. I was trying to init the constant array in the interface declaration where I establish my globals. It doesn't let me populate the array there.
On the other hand, the C syntax works perfectly inside the implementation declaration, when used locally within a method.
I could use NSArray, but it seemed overkill to alloc a memory region, initWithObjects, and then have to purge - all to make a 10 byte table.
I guess it makes sense that Objective C won't let me embed a code-like construct in the declaration area. But being new to the language that didn't jump out at me.
A constant C-style array needs to be a file scope static or a function local variable. Normally a file scope static would be the way to do this. The static initialization can't go in a class declaration.
I wouldn't worry about overkill in generating an NSArray, unless you have proof that this will be a problem. Your most precious resource is your time. Development time spent tracking down bugs in use of C-Style arrays is wasted if those bugs can't exist if you use an NSArray.
I had a similar problem as I am new to Objective-C. As others are saying, it is the same as the normal C but one difference. That is, you can use them as you expect, but not with Objective-C specific things.
I figured out that C variable initializers can be used in the class interface, too, but not between @interface and the {...} or within the "{...}".
Otherwise they can be declared as you do with normal C header files. They can be also declared outside the block surrounded by an @interface and the paring @end.
E.g. This is OK and the emptyCoordinate can be used in the @implementation:
Replies
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI also sometimes will use an NSArray or NSDictionary for the same purpose. I'll have an accessor that builds the NSArray/NSDictionary lazily. These objects have some advantages over C-style arrays.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeOn the other hand, the C syntax works perfectly inside the implementation declaration, when used locally within a method.
I could use NSArray, but it seemed overkill to alloc a memory region, initWithObjects, and then have to purge - all to make a 10 byte table.
I guess it makes sense that Objective C won't let me embed a code-like construct in the declaration area. But being new to the language that didn't jump out at me.
Thanks for the replies.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI wouldn't worry about overkill in generating an NSArray, unless you have proof that this will be a problem. Your most precious resource is your time. Development time spent tracking down bugs in use of C-Style arrays is wasted if those bugs can't exist if you use an NSArray.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomewhich would be accessed by simply MyListClass.list.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeNSArray *list = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], [NSNumber numberWithInt:5], nil];
We need to use ruby in iphone...
In RUBY is:
list = [1,2,3,4,5]
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI figured out that C variable initializers can be used in the class interface, too, but not between @interface and the {...} or within the "{...}".
Otherwise they can be declared as you do with normal C header files. They can be also declared outside the block surrounded by an @interface and the paring @end.
E.g. This is OK and the emptyCoordinate can be used in the @implementation:
Or:
static const CLLocationCoordinate2D emptyCoordinate = {-1.0, -1.0};@interface ...
...
@end
Or:
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome