Objective-C Language Modern Objective-C Literals

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

Modern Objective C provides ways to reduce amount of code you need to initialize some common types. This new way is very similar to how NSString objects are initialized with constant strings.

NSNumber

Old way:

NSNumber *number = [NSNumber numberWithInt:25];

Modern way:

NSNumber *number = @25;

Note: you can also store BOOL values in NSNumber objects using @YES, @NO or @(someBoolValue);

NSArray

Old way:

NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", [NSNumber numberWithInt:3], @"Four", nil]; 

Modern way:

NSArray *array = @[@"One", @"Two", @3, @"Four"];

NSDictionary

Old way:

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys: array, @"Object", [NSNumber numberWithFloat:1.5], @"Value", @"ObjectiveC", @"Language", nil];

Modern way:

NSDictionary *dictionary = @{@"Object": array, @"Value": @1.5, @"Language": @"ObjectiveC"};


Got any Objective-C Language Question?