Objective-C Language Container subscripting


Example

In modern Objective C syntax you can get values from NSArray and NSDictionary containers using container subscripting.

Old way:

NSObject *object1 = [array objectAtIndex:1];
NSObject *object2 = [dictionary objectForKey:@"Value"];

Modern way:

NSObject *object1 = array[1];
NSObject *object2 = dictionary[@"Value"];

You can also insert objects into arrays and set objects for keys in dictionaries in a cleaner way:

Old way:

// replacing at specific index
[mutableArray replaceObjectAtIndex:1 withObject:@"NewValue"];
// adding a new value to the end
[mutableArray addObject:@"NewValue"];

[mutableDictionary setObject:@"NewValue" forKey:@"NewKey"];

Modern way:

mutableArray[1] = @"NewValue";
mutableArray[[mutableArray count]] = @"NewValue";

mutableDictionary[@"NewKey"] = @"NewValue";