ios 检查 NSDictionary 是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17745336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:24:47  来源:igfitidea点击:

Check if NSDictionary is empty

iphoneiosobjective-cnsmutablearraynsdictionary

提问by Steaphann

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

我想检查 anNSDictionary是否为空。我正在这样做。

  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];
    }

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

但它总是在这条线上崩溃arrCities = [mutDictValues objectForKey:@"cities"];并出现以下错误:

-[__NSCFConstantString objectForKey:]:

Can someone help me with this ?

有人可以帮我弄这个吗 ?

回答by Balu

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];

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

[dictionary count];

EDIT:- use dictionaryForKey: method

编辑:-使用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"]);

回答by rashii

if ( [mutDictValues count] == 0 ) {
    //code here
}
else {
    //code here
}

After having your dic retrieved this should do

取回你的 dic 后,这应该做

回答by βhargav?

try this,

尝试这个,

if([myDict count] > 0)
    NSLog(@"Dictionary is not empty");
else
    NSLog(@"Dictionary is empty");

回答by vikingosegundo

Somewhere you treat a nsstring (a concrete subclass) as NSdictionary.

在某个地方,您将 nsstring(一个具体的子类)视为 NSdictionary。

回答by Aadil Keshwani

I had a bit different issue but it is related so i would like to share it here.

我有一个有点不同的问题,但它是相关的,所以我想在这里分享。

I was fetching the webservices & storing data in NSDictionary & again Fetching objectForKey which was Null. So the solution i found is as under

我正在获取网络服务并在 NSDictionary 中存储数据,然后再次获取为 Null 的 objectForKey。所以我找到的解决方案如下

NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];

    // NSLog(@"%@" , result);

    NSMutableDictionary *sup = [result objectForKey:@"keysupplied"];
    NSNull *n=[NSNull null];
    if ( sup ! = n ){
      //Your code if its not null
    }

The reason behind using NSNull was it was returning (NSNull *) when i debugged the application So i finally figured out this way.

使用 NSNull 的原因是当我调试应用程序时它正在返回 (NSNull *) 所以我终于想通了这种方式。

回答by William Falcon

BOOL containsKeyABC = [myDict: valueForKey:@"ABC"];

int items = dict.count;

if (items > 0) {
     //not empty
}

回答by Waseem Shah

try this code

试试这个代码

NSMutableDictionary *dict = ...

BOOL isEmpty = ([dict count] == 0);

回答by Amar

As most of the answers have correctly pointed out that you are passing un-recognized selector objectForKey:to a NSStringinstance instead of NSDictionary, hence observing exception

由于大多数答案都正确指出您将无法识别的选择器objectForKey:传递给NSString实例而不是NSDictionary,因此观察异常

-[__NSCFConstantString objectForKey:]:

Check NSUserDefaultsto see whether citiesreturns a dictionary or something else. You can do this by two ways

检查NSUserDefaults是否cities返回字典或其他内容。你可以通过两种方式做到这一点

I. NSLogall data in NSUserDefaults

一、NSLog所有数据在NSUserDefaults

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);

II. Check the plist file which store the NSUserDefaultsfrom the Application folder. Check this answerfor more details.

二、检查存储NSUserDefaults来自 Application 文件夹的 plist 文件。检查此答案以获取更多详细信息。

Hope that helps.

希望有帮助。