开发者

Objective C - How to add a method to an existing class?

How can i add a new method to NSString class. I need to create a method that get's开发者_如何学运维 called on a string and returns an NSDictionary. I know that i can simply create a function that gets a string and return an nsdictionary, but i want to know how to add it to an existing class.

NSString *myStr = @"some json string";
NSDictionary *dictionary = [myStr getJSONData];


You can use Objective-C categories. For example to add on to NSString define the following in a new .h/.m file:

@interface NSString (CategoryName)

-(NSString *) aNewMethod;

@end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜