Objective-C Language Modern Objective-C Container subscripting

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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";


Got any Objective-C Language Question?