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

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

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

16条回答
  •  不知归路
    2020-11-28 01:35

    Because nil cannot be stored in Foundation data structures NSNull is sometimes to represent a nil. Because NSNull is a singleton object you can check to see if NSNull is the value stored in dictionary by using direct pointer comparison:

    if ((NSNull *)[user objectForKey:@"myKey"] == [NSNull null]) { }
    

提交回复
热议问题