Literals
suggest changeModern Objective-C reduces the code needed to create some common objects. The syntax is similar to NSString literals.
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"};
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents