Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling CFNull objects in NSPropertyListSerialization

In my application, I am trying to serialize a server response dictionary and writing it to file system. But I am getting error "Property list invalid for format" for some responses. The reason is CFNull objects in the server response. Now, the server response will keep on changing so I do not have a definite way to remove CFNull objects (). Below is my code:

NSString *anError = nil;
NSData *aData = [NSPropertyListSerialization dataFromPropertyList:iFile format:NSPropertyListXMLFormat_v1_0 errorDescription:&anError];

What is the best way to tackle this issue? How can I remove all CFNull objects from server response in one shot?

like image 570
Abhinav Avatar asked Dec 10 '22 08:12

Abhinav


1 Answers

I had this problem with receiving a response from the Facebook SDK, so I implemented this method:

- (void)cleanDictionary:(NSMutableDictionary *)dictionary {
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    if (obj == [NSNull null]) {
        [dictionary setObject:@"" forKey:key];
    } else if ([obj isKindOfClass:[NSDictionary class]]) {
        [self cleanDictionary:obj];
    }
}];

}

That'll walk the hierarchy of the dictionary and turn all CFNulls into the empty string.

like image 129
Tap Forms Avatar answered Jan 04 '23 00:01

Tap Forms