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.
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)
;
Old way:
NSArray *array = [[NSArray alloc] initWithObjects:@"One", @"Two", [NSNumber numberWithInt:3], @"Four", nil];
Modern way:
NSArray *array = @[@"One", @"Two", @3, @"Four"];
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"};