16

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

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

-[__NSCFConstantString objectForKey:]:

Can someone help me with this ?

Steaphann
  • 2,777
  • 6
  • 49
  • 106
  • if (![dictTemp isKindOfClass:[NSDictionary class]]) { // do something DisplayAlert(@"No data found") } – kb920 Jul 19 '13 at 13:00

9 Answers9

25

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

EDIT:- use dictionaryForKey: method

NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"hi",@"one",nil];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"dic"];
NSDictionary *dictn = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dic"];
NSLog(@"%@",[dictn objectForKey:@"one"]);
Balu
  • 8,450
  • 2
  • 22
  • 41
8
if ( [mutDictValues count] == 0 ) {
    //code here
}
else {
    //code here
}

After having your dic retrieved this should do

itsji10dra
  • 4,605
  • 3
  • 37
  • 58
rashii
  • 512
  • 7
  • 17
2

try this,

if([myDict count] > 0)
    NSLog(@"Dictionary is not empty");
else
    NSLog(@"Dictionary is empty");
βhargavḯ
  • 9,694
  • 1
  • 36
  • 59
1
BOOL containsKeyABC = [myDict: valueForKey:@"ABC"];

int items = dict.count;

if (items > 0) {
     //not empty
}
Abdullah Shafique
  • 6,850
  • 8
  • 31
  • 68
William Falcon
  • 9,660
  • 13
  • 63
  • 109
1

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

vikingosegundo
  • 51,574
  • 14
  • 135
  • 174
1

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

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.

Aadil Keshwani
  • 1,385
  • 1
  • 18
  • 28
0

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

-[__NSCFConstantString objectForKey:]:

Check NSUserDefaults to see whether cities returns a dictionary or something else. You can do this by two ways

I. NSLog all data in NSUserDefaults

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

II. Check the plist file which store the NSUserDefaults from the Application folder. Check this answer for more details.

Hope that helps.

Community
  • 1
  • 1
Amar
  • 13,173
  • 7
  • 52
  • 70
0

try this code

NSMutableDictionary *dict = ...

BOOL isEmpty = ([dict count] == 0);
Waseem Shah
  • 2,209
  • 22
  • 23
0
if (myDict.count)
    NSLog(@"Dictionary is not empty");
else
    NSLog(@"Dictionary is empty");

Every number that is 0 equals to @NO.

Every number that is not 0 equals to @YES.

Therefore you can just pass the count into the if.

Redwolf
  • 520
  • 4
  • 17