I'm quite new to Objective-C. I have problems when creating array of objects. In java it is possible to make array of object and can access individual instances directly.
For example,
SomeClass[] instance = new SomeClass[10];
instance[i].var=10;
Is there any way to do like this in Objective-C? Can I access the instance variable in array of object directly using index? An example would be of more help. Thanks in advance
Using the Foundation Framework (which you almost certainly will be if you're using Objective-C):
NSString *object1 = @"an object";
NSString *object2 = @"another object";
NSArray *myArray = [NSArray arrayWithObjects:object1, object2, nil];
NSString *str = [myArray objectAtIndex:1];
Here, str
will be a reference to object 2 (which contains another object
). Note that the nil
'terminates' the list of objects in the array, and is required. If you want a mutable (modifiable) array:
NSString *object1 = @"an object";
NSString *object2 = @"another object";
NSMutableArray *myMutableArray = [NSMutableArray array];
[myMutableArray addObject:object1];
[myMutableArray addObject:object2];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With