Handling CFNull objects in NSPropertyListSerializa

2019-04-10 08:23发布

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?

4条回答
The star\"
2楼-- · 2019-04-10 08:48

I switched to NSKeyedArchiver option. Little slower than NSPropertyListSerialization but takes care of NSNull/CFNull objects.

查看更多
beautiful°
3楼-- · 2019-04-10 08:49

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-10 08:52

If you can't serialize it via the datafromPropertyList:... API, then it isn't a property list.

Either fix the server responses to barf up proper property lists or massage the data in your app such that it can be properly interpreted as a property list.

查看更多
爱情/是我丢掉的垃圾
5楼-- · 2019-04-10 08:55

I wrote a Category to clean up a dictionary :

-(NSDictionary*)cleanDictionary
{
    return [NSDictionary cleanDictionary:self];
}

+ (NSDictionary*)cleanDictionary:(NSDictionary *)dictionary
{
    NSMutableDictionary *dict = [NSMutableDictionary new];

    [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
    {
        if (obj == [NSNull null])
        {
            //dont add it
        }

        else if ([obj isKindOfClass:[NSDictionary class]])
        {
            [dict setObject:[self cleanDictionary:obj] forKey:key];
        }
        else if ([obj isKindOfClass:[NSArray class]])
        {
            [dict setObject:[NSDictionary cleanArray:obj] forKey:key];
        }
        else {
            [dict setObject:obj forKey:key];
        }
    }];

    return dict;
}

+(NSArray*)cleanArray:(NSArray*)array
{
    NSMutableArray* returnArray = [NSMutableArray new];

    for (NSObject *obj in array)
    {
        if (obj == [NSNull null])
        {
           //dont add it
        }
        if ([obj isKindOfClass:[NSDictionary class]])
        {
            [returnArray addObject:[NSDictionary cleanDictionary:(NSDictionary*)obj]];
        }
        else if ([obj isKindOfClass:[NSArray class]])
        {
            [returnArray addObject:[NSDictionary cleanArray:(NSArray*)obj]];
        }
        else
        {
            [returnArray addObject:obj];
        }
    }
    return returnArray;
}
查看更多
登录 后发表回答