Decoding with NSCoding: Does Value Exist For Key?
Asked Answered
P

1

11

When using NSCoding and decoding values, is there a way to tell 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 issue is that I have old versions of documents in my app that don't have the "myKey" value, and if they don't have it, using 0 for myInt (what happens if you decode a nonexistent key) is not the behavior I want. However, I can't just decode and check if myInt == 0, because it might legitimately be equal to 0.

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

Panther answered 14/8, 2012 at 17:38 Comment(0)
K
21

How about containsValueForKey?

Kinelski answered 14/8, 2012 at 17:40 Comment(1)
Derp. Thanks haha. I knew I was missing something, it seemed like the functionality I wanted was pretty standard functionality...Panther

© 2022 - 2024 — McMap. All rights reserved.