iOS Dev Nugget 3 Modern Objective C


Need to run a code review on your codebase? Hire me

There's a few modern features of Objective C you should be using in new code, if not existing code. They make code easier to write and improve readability.

  1. Forward function declarations for "private" functions aren't needed anymore.
  2. Use container and NSNumber literals:

    NSNumber num = @{1.2}; NSArray a1 = @[@"one", @"two"]; NSMutableArray a2 = [NSMutableArray arrayWithArray:@[@"one", @"two"]]; NSDictionary d1 = @{@"key1": @"value1", @"key2": @"value2"}; NSMutableDictionary* d2 = [NSMutableDictionary dictionaryWithDictionary:d1];

  3. Use container subscripting:

    NSArray a1 = @"[@"one", @"two"]; NSString one = a1[0]; NSDictionary d1 = @{@"key1": @"value1", @"key2": @"value2"}; NSString val = d1[@"key2"];

In Xcode, you can use Edit > Convert > To Modern Objective-C Syntax to help you update existing code.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No


Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues