ios NSDictionary 具有空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15199934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSDictionary having null value
提问by Kalyan Urimi
I have a problem when Extracting data from a Dictionary object. Count of the Dictionary is displaying as 1, but the one value it is displaying is null. I want to display a AlertView when there is no data in the Dictionary object. I thought of displaying the AlertView when the count is '0', but it returning '1'.
从 Dictionary 对象中提取数据时遇到问题。字典的计数显示为 1,但它显示的一个值为空。当 Dictionary 对象中没有数据时,我想显示 AlertView。我想在计数为“0”时显示 AlertView,但它返回“1”。
I am extracting this dictionary object from a WebService using JSON.
我正在使用 JSON 从 WebService 中提取这个字典对象。
{
"My Data" = null;
}
Used this code for getting the "My Data" value into the Dictionary variable Datas.
使用此代码将“我的数据”值放入字典变量数据中。
Datas = (NSDictionary *) [details objectForKey:@"My Data"];
if ([Datas count] == 0) {
//code for showing AlertView....
}
Please help me to display a UIAlertView when the Dictionary value having null....
请帮助我在 Dictionary 值为 null 时显示 UIAlertView ....
回答by dasblinkenlight
NSDictionary
and other collections cannot contain nil
values. When NSDictionary
must store a null
, a special value [NSNull null]
is stored.
NSDictionary
和其他集合不能包含nil
值。当NSDictionary
必须存储 a 时null
,存储一个特殊值[NSNull null]
。
Compare the value at @"My Data"
to [NSNull null]
to determine if the corresponding value is null
or not.
在比较值@"My Data"
来[NSNull null]
确定对应的值null
或不是。
// Since [NSNull null] is a singleton, you can use == instead of isEqual
if ([details objectForKey:@"My Data"] == [NSNull null]) {
// Display the alert
}
回答by Carl Veazey
Usually null values in JSON get parsed to NSNull
. So this condition should check for that as well as nil: if((details[@"My Data"] == nil) || (details[@"My Data"] == [NSNull null]))
通常 JSON 中的空值被解析为NSNull
. 所以这个条件应该检查那个以及零:if((details[@"My Data"] == nil) || (details[@"My Data"] == [NSNull null]))
回答by Paras Joshi
if ([[details objectForKey:@"My Data"] isEqual:[NSNull null]] || [[details objectForKey:@"My Data"] isEqualToString:@""]) {
UIAlertView *altTemp = [UIAlertView alloc]initWithTitle:@"Value Null" message:@"Your Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[altTemp show];
[altTemp release];
}