本文介绍了检查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是否为空
发布评论