检查NSDictionary是否为空 [英] Check if NSDictionary is empty

查看:1207
本文介绍了检查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是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆