iOS:为什么我不能将nil设置为NSDictionary值? [英] iOS: Why can't I set nil to NSDictionary value?
本文介绍了iOS:为什么我不能将nil设置为NSDictionary值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个非常基本的问题,但我想知道为什么我不能将nil指定为NSDictionary值?我在我的代码中有很多地方。如果 [q objectForKey:@text]
为nil,则App崩溃。
This might be very basic question but I was wondering why can't I assign nil as NSDictionary value? I have following statement many places in my code. If [q objectForKey:@"text"]
is nil then App is crashing.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:2];
[dict setObject:[q objectForKey:@"text"] forKey:@"text"];
在将字符分配给字典之前,我必须在每个地方检查nil。这是唯一正确的做法吗?我错过了一些明显的东西吗
I have to check everywhere for the nil before assigning it to dictionary. Is this the only correct way of doing? Am I missing something obvious?
if([q objectForKey:@"text"] != nil)
[dict setObject:[q objectForKey:@"text"] forKey:@"text"];
else
[dict setObject:@"" forKey:@"text"];
推荐答案
它想要一个实际的对象...使用 NSNull
It wants an actual object... use NSNull
这篇关于iOS:为什么我不能将nil设置为NSDictionary值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文