检查NSDictionary是否为空

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

我想检查 NSDictionary 是否为空。我是这样做的。

I want to check if an NSDictionary is empty. I am doing it like this.

mutDictValues = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dicValues"]mutableCopy]; NSLog(@"dictValues are %@",mutDictValues); if(mutDictValues == NULL){ arrCities = [[NSMutableArray alloc]init]; NSLog(@"no cities seleceted"); }else{ arrCities = [[NSMutableArray alloc]init]; arrCities = [mutDictValues objectForKey:@"cities"]; [self placeCities]; }

但是这条线上的alwasy崩溃 arrCities = [ mutDictValues objectForKey:@cities]; ,出现以下错误:

But it alwasy crashes on this line arrCities = [mutDictValues objectForKey:@"cities"]; with the following error:

-[__NSCFConstantString objectForKey:]:

有人可以帮我吗?

推荐答案

从NSUserDefaults检索字典值时,字典自动转换为字符串,这是崩溃的原因检查字典使用

While retrieving the dictionary values from NSUserDefaults that dictionary automatically converted into string that is the reason for getting crashed and for checking dictionary use [dictionary count];

编辑: - 使用dictionaryForKey:方法

NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"hi",@"one",nil]; [[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"dic"]; NSDictionary *dictn = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dic"]; NSLog(@"%@",[dictn objectForKey:@"one"]);

更多推荐

检查NSDictionary是否为空

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

发布评论

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

>www.elefans.com

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