It’s not that I’m so smart, it’s just that I stay with problems longer.
— Albert Einstein

KVC Accessors – The Quick Way

Posted: May 22nd, 2009 | Author: Thomas | Filed under: Apple, Development | Tags: , , , | No Comments »

Before the availibility of Objective-C 2.0 the usage of Key-Value Coding accessors required some boiler plate code. Objective-C 2.0 introduced the @property and @synthesize directives and reduced a lot of boiler plate code into two simple statements. Which is kind of nice.
Nevertheless there might be circumstances that force you to implement KVC accessors manually. But you do not have to type all the accessor methods code by yourself. Xcode can take care of this task. Here is how to do this.

Within you .h file, create the property you need for your application. Let us assume you need an array like this:


NSArray *someArray;

Select the whole line where you just defined your NSArray instance variable and go to the script menu (it is the one with the ancient scroll icon). Select the command

Script Menu > Code > Place Accessor Decls on Clipboard

Next, paste the declarations to your .h file. Select the whole line with your NSArray instance variable again, this time select the command

Script Menu > Code > Place Accessor Defs on Clipboard

and paste the content of the clipboard to your .m file in the @implementation part of your code.
While this is only a minor help for properties of the NSArray class, try the same for the NSMutableArray class. Now we are talking about time saving. Thanks, Xcode team!



Leave a Reply