Modern Objective-C

Xcode 4.4 was released on the 25th July and as an update doesn’t make many changes to the basic operation of what was already an excellent IDE. Code completion is more informative, with a new information strip at the bottom of the code completion window, but most tasks are unchanged from the 4.3 release.

Of more interest is the upgrade to an updated LLVM 4.0 compiler that partially supports what Apple call “modern” Objective-C syntax. Some features work now with Xcode 4.4 and iOS 5.1. Others, notably simplified array and dictionary subscripting require updates to Foundation classes like NSArray and NSDictionary and will not be available until the release of Xcode 4.5. Here are the ones you can start using right away:

Literals

The declaration style of literal strings is extended to cover arrays, dictionaries and numbers:

Old Style

NSArray *countries = [NSArray arrayWithObjects:@"UK", @"USA", nil];
NSDictionary *capitals = [NSDictionary
    dictionaryWithObjectsAndKeys:@"London", @"UK", @"Washington", @"USA", nil
];
NSNumber *days = [NSNumber numberWithInt:365];

Modern Style

NSArray *countries = @[@"UK", @"USA"];
NSDictionary *capitals = @{@"UK":@"London", @"USA":@"Washington"};
NSNumber *days = @365;

As you can see from these simple examples, the new syntax is more compact and (with a bit of practice) more readable.

Property Synthesis

For some time now, it has been possible to use @property and @synthesize to create properties backed by instance variables without explicitly defining the instance variable:

Old Style

@interface MyClass : NSObject
@property (nonatomic, assign) NSInteger counter;
...
@end

@implementation MyClass
@synthesize counter = _counter;
...
@end

Modern Objective-C syntax takes this a step further by eliminating the need for the synthesize in the implementation. The default behaviour is to create the instance variable with a leading underscore, as above.

New Style

@interface MyClass : NSObject
@property (nonatomic, assign) NSInteger counter;
...
@end

@implementation MyClass
... // No synthesize required
@end

This means that more keystrokes saved in the editor, although I think this “magic” can be confusing for programmers transitioning to Objective-C from other languages.

Refactoring

The changes described above are supported by a new refactoring menu in Xcode 4.4, which you can find as Edit -> Refactor -> Convert to Modern Objective-C syntax. This works out the changes required, showing the differences in a side-by-side comparison which you can apply with the option of taking a snapshot. Something that is very handy is that this refactoring menu can be applied as many times as you like. It’s almost like a tutorial for the new features until you become fluent with them.

My task for the remainder of the week is converting Learning Tree’s Building iPhone® and iPad® Applications: Extended Features course to the new syntax. An author’s work is never done–especially when Apple technology is involved!

Richard Senior

Type to search blog.learningtree.com

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.