开发者

How does JSONRepresentation of NSDictionary class works?

开发者 https://www.devze.com 2023-02-24 04:01 出处:网络
some time a开发者_运维百科go I was figuring out JSON handling in Objective-C (iPhone) and I found one confusing thing there.

some time a开发者_运维百科go I was figuring out JSON handling in Objective-C (iPhone) and I found one confusing thing there.

This call puzzles me:

NSString* jsonString = [jsonDict JSONRepresentation];

In fact jsonDict is an instance of NSDictionary class and according to NSDictionary Class Reference NSDictionary does not have this JSONRepresentation method. I feel cheated somewhere, I know that it works but can't figure out how.

Can anyone explain?

Thanks


This is an example of a "Category", which is a way to add methods to existing classes.

http://developer.apple.com/library/ios/#documentation/general/conceptual/DevPedia-CocoaCore/Category.html


Take a look at JSON Framework this method came from.


The framework puts a category on NSDictionary. Categories are used to add extra methods to a class. More info on how categories work an are implemented is in the docs. Now that you know the right term for it it should be easy to find.

0

精彩评论

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

关注公众号