How does JSONRepresentation of NSDictionary class works?
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.
精彩评论