检查 NSDictionary 中是否存在 key 是否为 null
我确实搜索了如何检查 NSDictionary 键是否存在并提出了解决方案。但它仍然向我抛出一个错误,说向键添加空值。 我不确定我的代码是否正确。如果有人对此有任何想法可以帮助我。
NSDictionary *result;
id myImageURL = [result objectForKey:@"url"];
if ((NSNull *)myImageURL == [NSNull null])
myImageURL = @"";
id myImage = [result objectForKey:@"image"];
if ((NSNull *)myImage == [NSNull null])
myImage = @"";
检查 null 是否不添加任何内容,如果不添加该值。但它仍然给我一个错误,不知道为什么。
/****OUTPUT*****/
2011-08-11 14:56:06.668 Tab_Table_Win[6510:207] RESULTS : {
image = "<UIImage: 0xbc332c0>";
url = "http://a3.twimg.com/profile_images/999228511/normal.jpg";
}
2011-08-11 14:56:06.669 Tab_Table_Win[6510:207] url : http://a3.twimg.com/profile_images/999228511/normal.jpg
2011-08-11 14:56:06.670 Tab_Table_Win[6510:207] IMage : <UIImage: 0xbc332c0>
/*****Breaks Here ***/
2011-08-11 14:56:06.876 Tab_Table_Win[6510:207] RESULTS : {
}
2011-08-11 14:56:06.878 Tab_Table_Win[6510:207] url : (null)
2011-08-11 14:56:06.879 Tab_Table_Win[6510:207] IMage : (null)
2011-08-11 14:56:06.881 Tab_Table_Win[6510:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
I did search on how to check if NSDictionary key exists or not and came up with the solution. But still it throws me an error saying adding null value to the key.
I am not sure if my code is correct or not. If anyone has any idea about this can help me.
NSDictionary *result;
id myImageURL = [result objectForKey:@"url"];
if ((NSNull *)myImageURL == [NSNull null])
myImageURL = @"";
id myImage = [result objectForKey:@"image"];
if ((NSNull *)myImage == [NSNull null])
myImage = @"";
Check if null add nothing and if not add the value. But it still gives me an error dont know why.
/****OUTPUT*****/
2011-08-11 14:56:06.668 Tab_Table_Win[6510:207] RESULTS : {
image = "<UIImage: 0xbc332c0>";
url = "http://a3.twimg.com/profile_images/999228511/normal.jpg";
}
2011-08-11 14:56:06.669 Tab_Table_Win[6510:207] url : http://a3.twimg.com/profile_images/999228511/normal.jpg
2011-08-11 14:56:06.670 Tab_Table_Win[6510:207] IMage : <UIImage: 0xbc332c0>
/*****Breaks Here ***/
2011-08-11 14:56:06.876 Tab_Table_Win[6510:207] RESULTS : {
}
2011-08-11 14:56:06.878 Tab_Table_Win[6510:207] url : (null)
2011-08-11 14:56:06.879 Tab_Table_Win[6510:207] IMage : (null)
2011-08-11 14:56:06.881 Tab_Table_Win[6510:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary setObject:forKey:]: attempt to insert nil key'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
正确答案是:
谢谢您的所有解释。
Correct answer is :
Thank you for all the explanation.
汤米完美地解释了这一点。
我建议创建 NSDictionary 类的扩展,例如:
和实现文件:
不要在代码中使用
[dictionary objectForKey:@"keyName"];
,而是使用这种方式,正如 Tommy 所解释的那样,您将向 nil 发送方法调用,这不会使应用程序崩溃,但您的对象将获得 nil 值。
希望这有帮助。
Tommy explained this perfectly.
What I recommend is create an extension of the NSDictionary class like:
And the implementation file:
And instead of using
[dictionary objectForKey:@"keyName"];
in your code, useThis way, as Tommy explained, you'd be sending a method call to a nil which wouldn't crash the app but your object would get a nil value.
Hope this helps.
每当我尝试检查从字典返回的对象是否为空时,我都会这样做:
然后在您的代码中,它将是:
这就是我将在您的代码中执行的操作。
另外,只需确定 NSDictionary 结果是否已定义?在您的代码中,它没有被设置为任何内容。它只是被定义为您计划使用调用结果的变量
Whenever I try to check if an object being returned from a dictionary is null, I do this:
Then in your code, it would be:
That's what I would do in your code.
Also, just making sure, is the NSDictionary result defined? In your code, it doesn't have anything it's being set to. It's just being defined as variable you plan on using called results
下面的答案对我有用:
https://stackoverflow.com/a/2784675/936957
另请注意,您可以获得使用字典中的键数组
the answer below worked for me:
https://stackoverflow.com/a/2784675/936957
Also note that you can get array of the keys in a dictionary using
如果某个键不存在对象,NSDictionary 将返回
nil
。NSNull
是一个实际的对象,因此是一个不同的东西。这就好比能够记录有一个值并且该值为null,和不记录是否有一个值之间的区别。它还取决于您用 C 术语来思考指向对象的指针的间接寻址而不仅仅是对象,因此从这个角度来看,它在语义上并不完全令人满意。在 Objective-C 中,您可以向
nil
发送任何消息,并且结果保证为nil
(或 0)。因此,如果您的代码旨在确保拥有安全的对象引用(就像在 C++ 中一样),那么您所做的就是不必要的。复合语句如下:始终显式安全,即使 alloc 失败并返回
nil
。所发生的情况是,对init
的调用根本不会执行任何操作,并且对象将以值nil
结束,因为的发送结果>init
到nil
也是nil
。话虽如此,比尔和伊曼纽尔提供的答案应该是正确的。将结果直接与
nil
或隐式与零进行比较。如果您稍后遇到崩溃,我猜这是因为您期望myImageUrl
和myImage
是NSString
以外的类型(我注意到您在原始代码中使用了无类型id
)并向他们发送了一条他们不回复的消息。If an object doesn't exist for a key, NSDictionary will return
nil
. AnNSNull
is an actual object, and therefore a distinct thing. It's like the distinction between being able to record that there was a value and the value as null, and not recording whether there was a value. It also rests a bit on you thinking in C terms of the indirection of a pointer to an object rather than just an object, so it's not completely semantically pleasing from that perspective.In Objective-C, you may send any message to
nil
and the result is guaranteed to benil
(or 0). So if your code is designed to ensure that you have a safe object reference, as you might in C++, then what you're doing is unnecessary. Compound statements like:Are always explicitly safe, even if alloc fails and returns
nil
. All that'll happen is that the call toinit
won't do anything at all, and object will end up with the valuenil
because the result of sending ofinit
tonil
is alsonil
.That being said, the answers provided by Bill and Emmanuel should be correct. Compare your result either directly to
nil
or implicitly to zero. If you're getting a crash later on, I'll guess it's because you're expectingmyImageUrl
andmyImage
to be types other thanNSString
(I notice you've used the typelessid
in your original code) and sending them a message they don't respond to.看看这是否有效,而不是过度考虑 NULL 类。
See if that works, rather than overthinking the NULL class.
这是另一个选择:
this another option:
这对我来说不起作用,我是这样想的
that was not work for me, i figured it out like this
好吧,这是@Iomec 几乎得到的实际答案
,这是实际的正确答案,因为它为空,当你说 myImage = [receivedObject...];那么如果 myImage = nil,您实际上将空值(nil)转换为一个类,如果不是正在运行的错误,那么这是一个异常。
你应该:
1)测试NSNull空值
2) 如果不是 nil,则分配
如果您的代码尚未出现错误,那么当您有一天在后台运行 8 个应用程序时,它将在生产环境中运行。
Alright here's the actual answer which @Iomec almost had
That is the actual correct answer because, it comes as null and when you say myImage = [receivedObject...]; then if myImage = nil, you are in effect casting a null value(nil) into a class which is an exception, if not a running bug.
You should:
1) test for NSNull null value
2) if not nil then assign
If you code hasn't bugged out yet, it will in production when you have 8 apps running in the background one day.
我在使用 JSONKit 时遇到了同样的问题。那里的实现是
所以如果对象不存在,这肯定会返回 NULL。我像下面这样检查
I got the same issue with JSONKit. The implementation there is
So this will definitely return NULL if the object isn't there. I check it like the following
可能想通过检查以确保它不是字符串而不是仅仅检查它是否为零来增加更多的安全性。 (以确保它不是数字或您可能不想要的任何其他内容。)
Might want to add a bit more safety by checking to make sure it is NOT a string instead of just checking if it IS a nil. (To make sure it is not a number or anything else you might not want.)
当您在可为空的字典中调用
objectForKey
时,应用程序会崩溃,因此我通过这种方式修复了此问题以避免崩溃。}
When you call
objectForKey
in nullable dictionary, app gets crashed so I fixed this from this way to avoid from crash.}