简体   繁体   English

iOS 8 setValuesForKeysWithDictionary在iPhone 5 / iPhone 4s上失败

[英]iOS 8 setValuesForKeysWithDictionary fails on iPhone 5 / iPhone 4s

I have code that deserializes some json to an object. 我有将一些json反序列化为对象的代码。 This code runs fine on iPhone 5s, iPhone 6, and iPhone 6+. 这段代码可以在iPhone 5s,iPhone 6和iPhone 6+上正常运行。

However, when running on an iPhone 5 or iPhone 4s, I get the error: [__NSCFString longValue]: unrecognized selector sent to instance 但是,在iPhone 5或iPhone 4s上运行时,出现错误: [__NSCFString longValue]: unrecognized selector sent to instance

Here's the code: 这是代码:

public class Serializable : NSObject
{        
    func deserialize(dictionary: NSMutableDictionary)
    {
        /*  Remove entries from the dictionary that do not have a corresponding property on the target object.
        By default, setValuesForKeysWithDictionary() will cause the app to crash if it encounters the above. */
        removeInvalidProperties(self, dictionary: dictionary)

        self.setValuesForKeysWithDictionary(dictionary)
    }        

    private func removeInvalidProperties(object: NSObject, dictionary: NSMutableDictionary)
    {
        for key in dictionary.allKeys {
            var exists: Bool = object.respondsToSelector(Selector(key as String))
            if !exists {
                dictionary.removeObjectForKey(key)
            }
        }
    }
}

I can't help but notice that the phones that run this code correctly are 64-bit architecture, and the ones that throw an error are 32-bit. 我忍不住注意到正确运行此代码的电话是64位体系结构,而抛出错误的电话是32位。 I can only assume that this is a runtime compatibility issue. 我只能假定这是运行时兼容性问题。

Does anyone know what might be causing this, or how to get around it? 有谁知道这可能是什么原因,或者如何解决?

One issue that could arise is if you had your properties mistyped when you were deserializing from JSON. 可能出现的一个问题是,当您从JSON反序列化时,如果您的属性输入错误。 I am fairly confident that the 64-bit runtime will automatically convert a string to an int but the 32-bit runtime will not. 我相当有信心64位运行时会自动将字符串转换为int,而32位运行时不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM