tags:

views:

40

answers:

2

How do I write my own "extension method" in objective-c?

http://code.google.com/p/json-framework/

This library does it and it works like this.

NSString *myString = ...;
id myResult = [myString JSONValue];

where the myResult returns an NSDictionary or NSArray.

What are these called? How do I write my own?

+4  A: 

You're looking for "categories". See the language guide chapter on the subject.

Jim Puls
Thanks. What a strange name for this language feature.
Daniel A. White
They're called categories, because as originally conceived, they weren't so much a way to add to classes as a way to keep methods organized into different functional categories. You might have a category to implement printing, for example, which wouldn't have to be loaded until and unless you tried to print a view.
NSResponder
+1  A: 

This is done by the use of categories. You can use categories to add methods to any class. See: http://devworld.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-TPXREF139

Example:

#import "ClassName.h"

@interface ClassName ( CategoryName )
// method declarations
@end
Chris Farber