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?

MikeS
  • 3,891
  • 6
  • 35
  • 51

1 Answers1

21

How about containsValueForKey?

Pang
  • 9,564
  • 146
  • 81
  • 122
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Derp. Thanks haha. I knew I was missing something, it seemed like the functionality I wanted was pretty standard functionality... – MikeS Aug 14 '12 at 17:42