NSCoding decoding: is there a value for the key?

When using NSCoding and decoding values, is there a way to find out if a value exists for a given key? In other words, what I'm trying to do is ...

if([decoder valueExistsForKey:@"myKey"]) //valueExistsForKey is not a real method :( { NSInteger *myInt = [decoder decodeValueForKey:@"myKey"]; } else { //handle special case } 

The problem is that I have old versions of documents in my application that don’t have a value of "myKey", and if they don’t have it, then 0 is used for myInt (what happens if you decode a nonexistent key) is not the behavior that I want. However, I cannot simply decode and check if myInt == 0, because it can be legal equal to 0.

Since the valueExistsForKey method does not seem to exist, how can I replicate this behavior?

+6
source share
1 answer

What about containsValueForKey ?

+15
source

Source: https://habr.com/ru/post/922865/


All Articles