Unlike some other languages, Objective-C makes a distinction between Arrays and Dictionaries.
An array is a collection, indexed by number. A dictionary is a collection indexed by objects (commonly strings, but can be other types).
On top of that, Objective-C makes a distinctions between mutable and immutable arrays and dictionaries. NSArray
and NSDictionary
cannot be modified after they are created. If you need to add objects to your collection, use NSMutableArray
and NSMutableDictionary
instead.
NSArray * myArray = [[NSArray alloc] initWithObjects: @"text1", @"text2", @"text3", nil];
// you can then access a member by using:
NSString * text = [myArray objectAtIndex: 0];
NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
// add an object
[myDict setObject:@"text1" forKey:@"key1"];
[myDict setObject:@"text2" forKey:@"key2"];
// you can retrieve objects using:
text = [myDict objectForKey: @"key1"];
[myArray release];
[myDict release];