Thought on good coding style in terms of @property in Objective-C

From Mac OS X 10.5, many things were changed silently. First, GCD, OpenCL and blocks were introduced to Objective-C/Cocoa. new behaviour of Category, property and so on.
We don’t need to mention about iOS to add more examples.
It seems to me that Apple tried to combine programming model of Mac OS X and iOS, but still I don’t see such thing happen.

By the way, there are very subtle and silent changes especially in Objective-C itself.
People who don’t read Objective-C Programming Language carefully would miss a few important changes like :

There are quite visible changes, but there are also not so visible changes.


@interface SampleClass {

    IBOutlet NSWindow *window;
}

@end

It was the previous way of declaring IBOutlet variable. In the declaration of the class, it can be easily determined which are IBOutlets and which are not.

Now, let’s take a look at this.


@interface SampleClass {

    NSWindow *window;
    NSWindows *windowTwo;
    NSWindows *windowThree;
}

@property( assign ) IBOutlet NSWindow *window;
@end

It is not easy to figure out what are IBOutlets and which are not. Although you can, by reading the @property lines, but there will be occasion where it is not easy due to long lines of declaration of your class, etc.

Then, it would be helpful to organize codes in comment blocks to differentiate what is what like :


@interface SampleClass {

    // IBOutlets
    NSWindow *window;

   //
    NSWindows *windowTwo;
    NSWindows *windowThree;
}

@property( assign ) IBOutlet NSWindow *window;
@end

However, because it seems to me that Objective-C 2.0 was changed two times, Objective-C allows creating a property without declaring it in the class, it can be done like :


@interface SampleClass {

   //
    NSWindows *windowTwo;
    NSWindows *windowThree;
}

@property( assign ) IBOutlet NSWindow *window;
@end

ADDED : The code above depends on the version of Xcode. With Xcode 3.2.6, whether I use GCC 4.2.2, LLVM 1.7, LLVM GCC 4.2, it didn’t work. I tested the code above with Xcode 4.
Recently there were many silent changes in Objective-C, so somehow it seems to me that the compilers bunlded with Xcode 4 have different Objective-C behaviour.

 

So, you can differentiate what is what.

Or you can still do this :


@interface SampleClass {

    IBOutlet NSWindow *window;
   //
    NSWindows *windowTwo;
    NSWindows *windowThree;
}

@property( assign ) IBOutlet NSWindow *window;
@end

But isn’t it too much typing?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: