Literals
suggest changeModern 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"};
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents