开发者

"Extension Methods" in Objective-C

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 = [my开发者_StackOverflow中文版String JSONValue];

where the myResult returns an NSDictionary or NSArray.

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


This is done by the use of categories. You can use categories to add methods to any class. See: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW1

Example:

#import "ClassName.h"

@interface ClassName ( CategoryName )
// method declarations
@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜