TouchJSON, dealing with NSNull

前端 未结 3 1520
清酒与你
清酒与你 2020-11-29 06:50

Hi I am using TouchJSON to deserialize some JSON. I have been using it in the past and on those occasions I dealt with occurrences of NSNull manually. I would think the auth

3条回答
  •  盖世英雄少女心
    2020-11-29 07:15

    nil and NULL are actually both equal to zero, so they are, in practice, interchangeable. But you're right, for consistency, the documentation for TouchJSON should have used theDeserializer.nullObject = nil instead of NULL.

    Now, when you do that, your second predicate actually works fine:

    if (![jsonDataDict objectForKey:someKey])
    

    because TouchJSON omits the key from the dictionary when you have nullObject set to nil (or NULL). When the key doesn't exist in the dictionary, NSDictionary returns nil, which is zero so your if condition works as you expect.

    If you don't specify nullObject as nil, you can instead check for null like so:

    if ([jsonDataDict objectForKey:someKey] == [NSNull null])
    

提交回复
热议问题