Created
October 20, 2011 20:40
-
-
Save greenisus/1302314 to your computer and use it in GitHub Desktop.
This file contains 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
- (id) serialize:(id) object{ | |
NSData *value = nil; | |
id serializedObject = nil; | |
if ([object isKindOfClass:[CKRecord class]]) { | |
CKRecord *record = (CKRecord *)object; | |
NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init]; | |
NSDictionary *attributes = [record attributes]; | |
for (NSString *name in attributes) { | |
// performSelector causes a warning, so using objc_msgSend instead | |
id attrValue = objc_msgSend(record, NSSelectorFromString(name)); | |
if ([attrValue isKindOfClass:[NSString class]] || [attrValue isKindOfClass:[NSNumber class]]) { | |
[jsonDictionary setValue:attrValue forKey:name]; | |
} | |
} | |
serializedObject = jsonDictionary; | |
} else { | |
serializedObject = object; | |
} | |
if([NSJSONSerialization isValidJSONObject:serializedObject]){ | |
NSError *error = nil; | |
value = [NSJSONSerialization dataWithJSONObject:serializedObject options:0 error:&error]; | |
if(error) | |
NSLog(@"%@", [error localizedFailureReason]); | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment