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

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

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

16条回答
  •  离开以前
    2020-11-28 01:35

    I'd suggest you store the result of the lookup in a temp variable, test if the temp variable is nil and then use it. That way you don't look the same object up twice:

    id obj = [dict objectForKey:@"blah"];
    
    if (obj) {
       // use obj
    } else {
       // Do something else
    }
    

提交回复
热议问题