The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 3,826,150
since: 19 Jan 2005

Leopardizing Hillegass' Book - Chapter 6 "Bindings and NSController"

posted Sat 17 Mar 07

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. 

tags:                    

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Ahruman left...
Sun 18 Mar 07 8:09 am

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.)

The {} isn’t needed if you have no ivars, and neither is importing both Cocoa and Foundation – the former includes the latter. Since you seem to be obsessed with low line counts… ;-)


2. Alpha Chen left...
Sun 18 Mar 07 12:14 pm :: http://blog.kejadlen.net/

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?


3. Kevin Hoffman left...
Sun 18 Mar 07 2:41 pm

Alpha - I would highly recommend Visual C# 2005 Unleashed, though i'm a little biased since I wrote it :)


4. Kevin Hoffman left...
Sun 18 Mar 07 2:42 pm

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)


5. Kevin Hoffman left...
Mon 19 Mar 07 6:19 am

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.

In the situation of the code sample in this chapter and the next few chapters (the RaiseMan sample), I am the only consumer of my code, and I will not be using an NSMutableString.. when I change the value of the string within the NSTableView, I am replacing it with an entirely new pointer, which causes KVO notifications to fire.


6. Jon left...
Fri 15 Jun 07 9:28 pm

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.


Tag Related Posts

Upgrading your Leopard install to Java SE 6 64-Bit

Mon 12 Jan 09 1:38 P GMT-05
tags:            

Apple drops the iPhone NDA for Released Software

Wed 01 Oct 08 3:54 P GMT-05
tags:          

Cappuccino, Objective-J, and You

Wed 10 Sep 08 6:14 P GMT-05

So I'm in the LA Times ;)

Wed 27 Aug 08 2:51 P GMT-05
tags:                  

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

My Macbook Air is masculine, dammit!

Mon 17 Mar 08 6:59 P GMT-05
tags:          

iPhone Underrated as a Gaming Device?

Fri 14 Mar 08 1:50 P GMT-05
tags:        

My take on the iPhone SDK

Sat 08 Mar 08 1:39 P GMT-05

Jobs says "not likely" to Flash on the iPhone

Thu 06 Mar 08 1:39 A GMT-05
tags:          

My Macbook Air Review

Sun 02 Mar 08 4:20 P GMT-05

iPhone Roadmap March 6th

Fri 29 Feb 08 10:41 P GMT-05
tags:        

Video of the Macbook Air in Action

Wed 20 Feb 08 3:04 P GMT-05

Macbook Airはきれいですよ!

Sun 17 Feb 08 2:38 A GMT-05

Why is O'Reilly Condoning iPhone Hacking?

Mon 11 Feb 08 3:55 P GMT-05

Evaluating my next laptop purchase

Wed 06 Feb 08 8:40 P GMT-05

The iPhone SDK key has been leaked! Oh Noez!!!1

Tue 29 Jan 08 11:36 A GMT-05
tags:        

Why Geeks just don't "get" the Macbook Air

Thu 17 Jan 08 2:30 P GMT-05

Popcorn + TiVo + Macbook Pro + iPhone == Hell Yeah!

Tue 15 Jan 08 3:11 P GMT-05
tags:          

How my ADC membership changed my life

Mon 31 Dec 07 3:57 P GMT-05
tags:      

Leopard Code Sample : Sprinkling in some Bonjour

Tue 27 Nov 07 2:32 P GMT-05
tags:        

Leopard Sample: A Bound NSCollectionView

Mon 29 Oct 07 1:41 A GMT-05

Leopard is out - let the code samples begin!

Fri 26 Oct 07 10:09 A GMT-05
tags:          

My life is complete : iPhone SDK is CONFIRMED.

Wed 17 Oct 07 6:38 P GMT-05
tags:          

Leopard Shipping October 26th!!

Tue 16 Oct 07 4:59 P GMT-05
tags:        

My iPhone Review

Mon 23 Jul 07 1:09 P GMT-05
tags:        

Microsoft Codename Acropolis - Unwrapped

Wed 20 Jun 07 3:22 P GMT-05
tags:              

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

I got on the endcap baby!

Sun 10 Jun 07 1:06 A GMT-05
tags: