您的位置:首页 > 移动开发 > Objective-C

Objective-C dictionaryWithObjectsAndKeys 问题

2015-03-09 15:53 447 查看
在使用 dictionaryWithObjectsAndKeys 初始化 NSMutableDictionary

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:value1, @"key1", value2, @"key2", value3, @"key3", nil];


可能会发现 dict 中没有3个key-value对,原因是 dictionaryWithObjectsAndKeys 遇到 object 为 nil 时结束。

解决方式是在 object 可能为 nil 下, 使用 setObject:forKey:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:10];
[dict setObject:value1 forKey:@"key1"];
[dict setObject:value2 forKey:@"key2"];
[dict setObject:value3 forKey:@"key3"];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: