Tutorial by Examples

Subscripts can be used to simplify retrieving and setting elements in an array. Given the following array NSArray *fruit = @[@"Apples", @"Bananas", @"Cherries"]; This line [fruit objectAtIndex: 1]; Can be replaced by fruit[1]; They can also be used to set an...
Subscripts can also be used with NSDictionary and NSMutableDictionary. The following code: NSMutableDictionary *myDictionary = [@{@"Foo": @"Bar"} mutableCopy]; [myDictionary setObject:@"Baz" forKey:@"Foo"]; NSLog(@"%@", [myDictionary objectForKey:...
You can add subscripting to your own classes by implementing the required methods. For indexed subscripting (like arrays): - (id)objectAtIndexedSubscript:(NSUInteger)idx - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx For keyed subscripting (like dictionaries): - (id)objectForKey...

Page 1 of 1