【问题标题】:Decoding with NSCoding: Does Value Exist For Key?使用 NSCoding 解码:键值是否存在?
【发布时间】:2012-08-14 17:38:11
【问题描述】:

在使用 NSCoding 和解码值时,有没有办法判断给定键的值是否存在?换句话说,我想做的是……

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

问题是我的应用中有旧版本的文档没有“myKey”值,如果它们没有,则使用 0 表示 myInt(如果解码不存在的密钥会发生什么情况)不是我想要的行为。但是,我不能只解码并检查 myInt == 0,因为它可能合法地等于 0。

由于 valueExistsForKey 方法似乎不存在,我该如何复制这种行为?

【问题讨论】:

    标签: objective-c nscoding


    【解决方案1】:

    containsValueForKey怎么样?

    【讨论】:

    • 德普。谢谢哈哈。我知道我遗漏了一些东西,我想要的功能似乎是非常标准的功能......
    猜你喜欢
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    相关资源
    最近更新 更多