Created
February 9, 2013 05:24
-
-
Save romyilano/4743988 to your computer and use it in GitHub Desktop.
Thanks to one of the raywenderlich.com authors ->
here's a nice NSDIctionary category extension that easily turns json data into convenient dictionaries toJSON: can be used on an NSDIctionary instance to get JSON data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // extend NSdictionary (foundation) class | |
| @interface NSDictionary(JSONCategories) | |
| +(NSDictionary *)dictionaryWithContentsOfJSONURLString:(NSString *)urlAddress; | |
| -(NSData *)toJSON; | |
| @end | |
| @implementation NSDictionary(JSONCategories) | |
| +(NSDictionary *)dictionaryWithContentsOfJSONURLString:(NSString *)urlAddress | |
| { | |
| NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlAddress]]; | |
| __autoreleasing NSError *error = nil; | |
| id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; | |
| if (error !=nil) return nil; | |
| return result; | |
| } | |
| // use this to call on an NSDictionary instnace to get JSON Data out of it | |
| -(NSData *)toJSON | |
| { | |
| NSError *error = nil; | |
| id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error]; | |
| if(error !=nil ) return nil; | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment