如何从NSDictionary中删除空值 [英] How to remove a null value from NSDictionary

查看:815
本文介绍了如何从NSDictionary中删除空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON Feed:

I have a JSON Feed:

{
            "count1" = 2;
            "count2" = 2;
            idval = 40;
            level = "<null>";
            "logo_url" = "/assets/logos/default_logo_medium.png";
            name = "Golf Club";
            "role_in_club" = Admin;
}

问题是< null> 我无法弄清楚如何在将NSDictionary保存到NSUserDefaults之前从NSDictionary中删除。

The problem is the "<null>" that I cannot figure out how to remove from the NSDictionary before saving it to NSUserDefaults.

请帮我解决这个问题。谢谢!

Please help me solve this issue. Thankyou!

推荐答案

遍历字典并查找任何空条目并将其删除。

Iterate through the dictionary and look for any null entries and remove them.

NSMutableDictionary *prunedDictionary = [NSMutableDictionary dictionary];
for (NSString * key in [yourDictionary allKeys])
{
    if (![[yourDictionary objectForKey:key] isKindOfClass:[NSNull class]])
        [prunedDictionary setObject:[yourDictionary objectForKey:key] forKey:key];
}

之后, prunedDictionary 应该在原始字典中包含所有非空项目。

After that, prunedDictionary should have all non-null items in the original dictionary.

这篇关于如何从NSDictionary中删除空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆