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

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

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

16条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 01:42

    I like Fernandes' answer even though you ask for the obj twice.

    This should also do (more or less the same as Martin's A).

    id obj;
    
    if ((obj=[dict objectForKey:@"blah"])) {
       // use obj
    } else {
       // Do something else like creating the obj and add the kv pair to the dict
    }
    

    Martin's and this answer both work on iPad2 iOS 5.0.1 9A405

提交回复
热议问题