|
So I've been going back through Aaron Hillegass' book on Cocoa programming (quite possibly the best book I've encountered on the subject, btw). One of the things I thought would be worthwhile for me to do would be to try and upgrade the code samples in his book to use as many native Objective-C 2.0 features as possible.
In this case, I am using the property declaration syntax shortcuts for the model objects. Take a look at the code Aaron wrote for the Person object for the model in his MVC pattern implementation:
#import
@interface Person: NSObject {
NSString *personName;
float expectedRaise;
}
- (float)expectedRaise;
- (void)setExpectedRaise:(float)theRaise;
- (NSString *)personName;
- (void)setPersonName:(NSString *)aPersonName;
@end
That's just the stuff in the header file. The Person.m file actually contains the implementation methods for the getters (expectedRaise and personName) and the setters (setExpectedRaise and setPersonName). In current versions of Cocoa, the setter methods also need to manage retain counts. For example, here's what the Tiger version of setPersonName looks like:
- (void)setPersonName:(NSString *)aPersonName
{
aPersonName = [aPersonName copy];
[personName release];
personName = aPersonName;
}
To me, someone who despises the getter/setter syntax in Java, and someone who, for the last 6+ years, has not had to maintain retain/release counts due to running in a garbage collected environment - this looks pretty ugly.
That's why I'm loving Leopard and Objective-C 2.0 so much. Not only do we get true property accessor syntax, complete with the "dot" syntax instead of the "message" syntax, but we also get easy property declarations (keep in mind that Tiger Objective-C doesn't have any clue what a property is...). But wait, there's more! We also get shortcuts that allow Cocoa to auto-implement the property accessors for us, AND, those property accessors do not need to maintain retain/release counts or even use an autorelease pool!!
So, given the heavy header file I showed you, and the 2-page .m file that you can see in his book, take a look at the entire implementation of the Person class in Leopard:
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject {
}
@property(ivar) NSString *personName;
@property(ivar) float expectedRaise;
@end
That's it. That's all there is. There is no code in the .m file, I didn't have to declare the instance variables (ivars), which, to us C# folks, would be the equivalent of the private backing members that support public or protected properties. So, in a single line of code, I've created an ivar (private member), I've created a property, and, I've had Objective-C create the get/set pair automatically on my behalf, and that get/set pair doesn't need to maintain retain/release counts (provided I'm building in GC mode).
Look famliar? :
public class Person
{
public string PersonName;
public float ExpectedRaise;
}
Hmm. Anybody questioning whether Objective-C 2.0 isn't on par (syntax-wise) with C# obviously isn't looking closely enough.
p.s. In case you're worried I'm blowing the NDA on Leopard, rest easy. The @property syntax is publicly visible in multiple locations, including some public documents available on Apple's website, so I'm not breaking any NDA there. However, if you're looking for a screenshot of my application, there's a reason I didn't post one :)
p.p.s. I apologize for the ugly formatting on this blog entry..The blog software on this host isn't 100% compatible with Safari.
The “personName” property should be declared copying to maintain the
original semantics. (The book presumably explains this, but the point is
that if someone sets to a mutable string and then changes that string, you
generally don’t want your property changing without notification.)
As someone who's going in the other direction (from Cocoa/Obj-C to
.NET/C#), this series of articles have been quite interesting to me!
However, speaking of Programming for OS X, I've been looking for a .NET
equivalent, and I was wondering if you have any recommendations?
Alpha - I would highly recommend Visual C# 2005 Unleashed, though i'm a
little biased since I wrote it :)
Ahruman - I'm not obsessed with low line counts, I just like code that's
easily readable. Also, I dislike having to perform the same menial task
over and over and over (such as writing "do nothing" accessor methods)
The situation Ahruman is describing is this: If you drop an NSMutableString
into the key-value-observed property, the system will see the initial
change, but subsequent changes to the underlying content of the string will
(I haven't tested this on Leopard, but it should hold true still) not cause
a key-value-observer notification because the pointer held by the
property/ivar remains the same.
Hate to add this so late, as I'm sure you already know it, but you don't
need to #include Foundation when you've already #include'd Cocoa, since
Cocoa is really just an umbrella framework for Foundation and AppKit.