复制没有空值的NSDictionary吗? [英] Copying an NSDictionary without empty values?

查看:47
本文介绍了复制没有空值的NSDictionary吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 dictionaryWithDictionary 方法创建一个 NSDictionary .但是,我的第一个 NSDictionary A可能有一些带有空值的键.如何创建不包含任何带有空值的键的B?我这样做是为了向 UITableView 填充与 NSDictionary 中每个非空键相对应的行.

I'm trying to create an NSDictionary with the dictionaryWithDictionary method. However, my first NSDictionary A may have some keys with empty values. How can I create B that doesn't have any keys with empty values? I'm doing all of this for the purposes of populating a UITableView with rows that correspond to each non-empty key in the NSDictionary.

推荐答案

这样的事情如何?

NSMutableDictionary *destDictionary = [NSMutableDictionary dictionaryWithCapacity:[sourceDictionary count]];
NSEnumerator *keyEnumerator = [[sourceDictionary allKeys] objectEnumerator];
id key;
while ( key = [keyEnumerator nextObject] )
    {
    NSString *object = [sourceDictionary objectForKey:key];
    if ([object length] == 0)
        continue;
    [destDictionary setObject:object forKey:key];
    }

这篇关于复制没有空值的NSDictionary吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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