Last Updated: February 25, 2016
·
2.988K
· csquirrel

Boxed Expressions In Objective-C

As seen here: http://mobiledevelopertips.com/objective-c/boxed-expressions-in-objective-c.html

// Float literal
NSNumber *floatNumberOld = [NSNumber numberWithFloat:999.99f];
NSNumber *floatNumberNew = @999.99;

// Double literal
NSNumber *piOld = [NSNumber numberWithDouble:3.1415926535];
NSNumber *piNew = @3.1415926535;

// Integer literal
NSNumber *intNumberOld = [NSNumber numberWithInt:99];
NSNumber *intNumberNew = @99;

// Unsigned integer literal
NSNumber *unsignedIntNumberOld = [NSNumber numberWithUnsignedInt:999];
NSNumber *unsignedIntNumberNew = @999;

// Long integer
NSNumber *longNumberOld = [NSNumber numberWithLong:1212l];
NSNumber *longNumberNew = @121212l;

// Long long integer
NSNumber *longLongNumberOld = [NSNumber numberWithLongLong:2323232323ll];
NSNumber *longLongNumberNew = @2323232323ll;

// Boolean literal
NSNumber *booleanNumberOld = [NSNumber numberWithBool:YES];
NSNumber *booleanNumberNew = @YES;
NSNumber *booleanNumberNew = @(YES);

// Charater literal
NSNumber *charNumberOld = [NSNumber numberWithChar:'A'];
NSNumber *charNumberNew = @'A';

// Literal format for empty array
NSArray *emptyArray = @[];

// Literal format for single element array
NSArray *singleElementArray = @[@"Error Message"];

// Literal format for array of strings
NSArray *arrayOfStrings = @[@"First Name", @"Last Name"];

// Literal format for array of objects
NSArray *arrayOfObjects = @[singleElementArray, arrayOfStrings, singleElementArray];

// Create empty dictionary
NSDictionary *emptyDictOld = [NSDictionary dictionary];
// New literal style
NSDictionary *emptyDictNew = @{};

// Create dictionary, one key and one object
NSDictionary *dictOneKeyOneObjectOld = 
   [NSDictionary dictionaryWithObject:@"StringObject" forKey:@"Key"];
// New literal style * notice key-object (not object-key)
NSDictionary *dictOneKeyOneObjectNew = @{@"Key" : @"StringObject"};

// Create dictionary, multiple keys and objects
NSDictionary *dictKeysObjectsOld = [NSDictionary dictionaryWithObjectsAndKeys:
                      // Object and key pairs
                      @"Beer", @"DrinkKey",
                      @"Stout", @"StyleKey",
                      @"Jet Black", @"ColorKey",
                      nil];

// New literal style * notice key-object (not object-key)
NSDictionary *dictKeysObjectsNew = @{@"DrinkKey" : @"Beer",
                      @"StyleKey" : @"Stout",
                      @"ColorKey" : @"Jet Black"};