Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JSONRepresentation of NSDictionary class works?

some time ago 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

like image 407
Burjua Avatar asked Apr 10 '11 17:04

Burjua


3 Answers

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

like image 112
Dave DeLong Avatar answered Nov 15 '22 07:11

Dave DeLong


Take a look at JSON Framework this method came from.

like image 40
Denis Mikhaylov Avatar answered Nov 15 '22 05:11

Denis Mikhaylov


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.

like image 41
Johan Kool Avatar answered Nov 15 '22 07:11

Johan Kool