检查NSDictionary中是否存在密钥为null

编程入门 行业动态 更新时间:2024-10-10 02:20:09
本文介绍了检查NSDictionary中是否存在密钥为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我确实搜索了如何检查NSDictionary密钥是否存在,并提出了解决方案。但它仍然会给我一个错误,即向键添加空值。 我不确定我的代码是否正确。如果有人对此有任何想法可以帮助我。

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 = @"";

检查null是否添加任何内容,如果没有添加值。但它仍然给我一个错误,不知道为什么。

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 = "a3.twimg/profile_images/999228511/normal.jpg"; } 2011-08-11 14:56:06.669 Tab_Table_Win[6510:207] url : a3.twimg/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'

推荐答案

正确答案是:

NSDictionary *result; NSURL *myImageURL = [result objectForKey:@"url"]; UIImage *myImage = [result objectForKey:@"image"]; /**** Correct way ****/ if (myImageURL != nil && myImage != nil) { [images setObject:myImage forKey:myImageURL]; }

感谢您提供所有解释。

更多推荐

检查NSDictionary中是否存在密钥为null

本文发布于:2023-11-09 11:47:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   是否存在   NSDictionary   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!