How to check if an NSDictionary or NSMutableDictionary contains a key?

前端 未结 16 2282
北海茫月
北海茫月 2020-11-28 00:52

I need to check if an dict has a key or not. How?

相关标签:
16条回答
  • 2020-11-28 01:45

    More recent versions of Objective-C and Clang have a modern syntax for this:

    if (myDictionary[myKey]) {
    
    }
    

    You do not have to check for equality with nil, because only non-nil Objective-C objects can be stored in dictionaries(or arrays). And all Objective-C objects are truthy values. Even @NO, @0, and [NSNull null] evaluate as true.

    Edit: Swift is now a thing.

    For Swift you would try something like the following

    if let value = myDictionary[myKey] {
    
    }
    

    This syntax will only execute the if block if myKey is in the dict and if it is then the value is stored in the value variable. Note that this works for even falsey values like 0.

    0 讨论(0)
  • 2020-11-28 01:46

    One very nasty gotcha which just wasted a bit of my time debugging - you may find yourself prompted by auto-complete to try using doesContain which seems to work.

    Except, doesContain uses an id comparison instead of the hash comparison used by objectForKey so if you have a dictionary with string keys it will return NO to a doesContain.

    NSMutableDictionary* keysByName = [[NSMutableDictionary alloc] init];
    keysByName[@"fred"] = @1;
    NSString* test = @"fred";
    
    if ([keysByName objectForKey:test] != nil)
        NSLog(@"\nit works for key lookups");  // OK
    else
        NSLog(@"\nsod it");
    
    if (keysByName[test] != nil)
        NSLog(@"\nit works for key lookups using indexed syntax");  // OK
    else
        NSLog(@"\nsod it");
    
    if ([keysByName doesContain:@"fred"])
        NSLog(@"\n doesContain works literally");
    else
        NSLog(@"\nsod it");  // this one fails because of id comparison used by doesContain
    
    0 讨论(0)
  • 2020-11-28 01:47

    As Adirael suggested objectForKey to check key existance but When you call objectForKeyin nullable dictionary, app gets crashed so I fixed this from following way.

    - (instancetype)initWithDictionary:(NSDictionary*)dictionary {
    id object = dictionary;
    
    if (dictionary && (object != [NSNull null])) {
        self.name = [dictionary objectForKey:@"name"];
        self.age = [dictionary objectForKey:@"age"];
    }
    return self;
    

    }

    0 讨论(0)
  • 2020-11-28 01:51

    Solution for swift 4.2

    So, if you just want to answer the question whether the dictionary contains the key, ask:

    let keyExists = dict[key] != nil
    

    If you want the value and you know the dictionary contains the key, say:

    let val = dict[key]!
    

    But if, as usually happens, you don't know it contains the key - you want to fetch it and use it, but only if it exists - then use something like if let:

    if let val = dict[key] {
        // now val is not nil and the Optional has been unwrapped, so use it
    }
    
    0 讨论(0)
提交回复
热议问题