Curious about what’s new in ObjC2? It’s pretty annoying being told “hey, we’re changing around the syntax of the language you do all your work in; hush, we’ll tell ya about it later” even when one has a seed key, so I looked around a bit.
The secrets lie in Apple’s branch of gcc in the gnu.org repositories, where the changelogs are quite telling. Especially in the testing suites. So following is a list of ObjC 2.0 additions and places where you can see them in the repository.
Don’t kill me, Apple Legal, this is all from public repositories.
Properties
This is the biggest addition to the language, at least as far as I can find. It’s going to save us hundreds of lines and hours upon hours of glue-code-writing.
Kind of like attributes in Ruby, Obj-C 2.0 allows programmers to define properties on classes. One can specify some attributes of those properties (nocopy, readonly, readwrite, etc), and then the compiler will generate appropriate KVC-compliant accessors for you. Or, if you like, you can just override the setVar: and var methods for additional functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | @interface Person : NSObject @property NSString *firstName, *lastName; @property(readonly) NSString *fullName; @end @interface Group : NSObject @property Person *techLead, *runtimeGuru, *propertiesMaven; @end @implementation Person - (NSString*)fullName { // This is a computed getter: it doesn't just mirror an ivar // like firstName and lastName. return [NSString stringWithFormat:@"%@ %@", firstName, lastName]; } @end @implementation Group - init { techLead = [[Person alloc] init]; runtimeGuru = [[Person alloc] init]; propertiesMaven = [[Person alloc] init]; return self; } @end void playWithProperties() { Group *g = [[Group alloc] init]; g.techLead.firstName = @"Blaine"; g.techLead.lastName = @"Garst"; g.runtimeGuru.firstName = @"Greg"; g.runtimeGuru.lastName = @"Parker"; g.propertiesMaven.firstName = @"Patrick"; g.propertiesMaven.lastName = @"Beard"; } |
A couple things are immediately noticeable:
- Properties are accessed with a period. A little odd, given that fast IV access uses
->. But it makes sense as a parallel to KVC paths. - The default property implementation seems to “do the right thing” even with objects like strings. It makes a copy on set, and it looks like it will clean up after itself too. At least, none of the tests have deallocs.
- Obviously, even with the properties, the Person objects in Group still had to be initialized.
There are other attributes properties can have. One useful example is ivar. It lets you map an external property name to an internal IV name:
@interface Bar : Object { int iVar; } @property(ivar = iVar) int FooBar; @end
You can also specify different getters and setters:
@property(getter = whatBaby, setter = setFire:) id baby;
Fast Enumeration with foreach
It looks like a language-based enumerator has been added. Here’s an example of usage:
NSArray *someArray; for (id obj in someArray) NSLog(@"%@", obj);
The standard Cocoa collections all have this working by default, of course, but if you need to use it on your own objects, that entails implementing:
- (unsigned int)countByEnumeratingWithState:objects:count:
Optional/required Protocol Methods
(thanks to Jay Tuley; I’d missed this!)
Like @public and @private, protocols now have @optional and @required keywords. Excellent! Before, this had only been a feature of informal protocols, so I’d found myself using NSObject categories all the time instead of real protocols to give the delegate greater flexibility.
Garbage Collection Metadata
The one other addition I can find in the public repositories is for garbage collection. __weak and __strong keywords have been added to specify weak or strong references for variables. You might use it like:
__weak NSString *someStringElsewhereMethod Attributes
Methods can be given attributes. Two usages I’ve seen so far in the repository are “deprecated” and “unavailable”, both of which will throw a suitable compiler warning if you try to call them. The keyword is __attribute__ ((attr)). Here’s a deprecated method prototype:
- (int)foo __attribute__ ((deprecated));
These already existed for C-style functions, but they are new to Obj-C selectors. A full list of attributes is available here.
Fast IV Access and IV Security
In the original version of this article, I introduced these as new features of ObjC2, but that’s not the case. Anyway, I didn’t know about them, so maybe they’ll be new to you, too.
These two kind of go hand in hand. Instance variables can be accessed with obj->iv style syntax, but only if they’re designated as @public. There are also @private and @protected keywords. Here’s a (modified) example from the repository:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @interface MySuperClass { @private int private; @protected int protected; @public int public; } @end @interface MyClass : MySuperClass @end int main (void) { MyClass *m = nil; access = m->private; /* error: private */ access = m->protected; /* error: protected */ access = m->public; /* Ok */ m->public = 2; /* Ok */ return 0; } |
Conclusion
Apple’s added a lot of really great stuff to the language—I’m especially excited about properties and fast enumeration. But they’ve also missed a huge opportunity to add closures to the language, which would have made it truly exceptional. Okay, that would have been really hard from the compiler / runtime side, but still.









Got Thoughts?
By all means share them, and start the conversation.
Leave a Comment
You can follow any responses to this entry via its RSS comments feed.