Reputation: 4538
As far as my study is concern for Objective C, I found this code for displaying the array elements:
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"a", @"b", @"c"];
int x = 0;
for(; x<[myArray count]; x++)
NSLog(@"value at index %i is %@", x, [myArray objectAtIndex:x]);
I am just curious if this is possible below: using [myArray[x]]
instead of [myArray objectAtIndex:x]
instead
for(; x < [myArray count]; x++)
NSLog(@"value at index %i is %@", x, [myArray[x]]);
Upvotes: 0
Views: 3513
Reputation: 29985
You cannot, and the reason is fairly simple: Objective-C is based on C and retains backwards compatibility.
In C, using [i]
on an object will call the []
operator of that object. However, on the local scope all Objective-C objects are simply pointers, which would make the call something similar to int[3]
. Does that look valid to you?
Implementing []
for NSArray
would mean breaking backwards compatibility.
Upvotes: 1
Reputation: 536027
No, it isn't. An NSArray is not a C array; it's an object like any other object and you have to talk to it the way you'd talk to any other object, using full-fledged message syntax. (The only "shortcut" of the sort you're looking for is properties.) I'm sorry, but Objective-C is horribly verbose, and this is an excellent case in point. You'll become accustomed to using code completion as much as possible, to avoid typing all those letters...
Upvotes: 1