处理 NSPropertyListSerialization 中的 CFNull 对象

发布于 2024-11-13 01:22:04 字数 375 浏览 0 评论 0原文

在我的应用程序中,我尝试序列化服务器响应字典并将其写入文件系统。但对于某些响应,我收到错误“属性列表格式无效”。原因是服务器响应中的 CFNull 对象。现在,服务器响应将不断变化,因此我没有明确的方法来删除 CFNull 对象 ()。下面是我的代码:

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

解决这个问题的最佳方法是什么?如何一次性从服务器响应中删除所有 CFNull 对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那片花海 2024-11-20 01:22:04

我在接收来自 Facebook SDK 的响应时遇到了这个问题,所以我实现了这个方法:

- (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];
    }
}];

}

这将遍历字典的层次结构并将所有 CFNull 转换为空字符串。

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.

初心未许 2024-11-20 01:22:04

我切换到 NSKeyedArchiver 选项。比 NSPropertyListSerialization 慢一点,但可以处理 NSNull/CFNull 对象。

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

苍白女子 2024-11-20 01:22:04

我写了一个类别来清理字典:

-(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;
}

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;
}
段念尘 2024-11-20 01:22:04

如果您无法通过 datafromPropertyList:... API 序列化它,那么它就不是属性列表。

要么修复服务器响应以生成正确的属性列表,要么修改应用程序中的数据,以便可以将其正确解释为属性列表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文