Swift Can';斯威夫特救不了布尔

Swift Can';斯威夫特救不了布尔,swift,nscoding,Swift,Nscoding,我是Swift(C++背景)的新手,我正在尝试做一件非常简单的事情:保存一个Bool。如果我将bool转换为“a”或“B”的字符串,然后再转换回来,那么效果会非常好。但是如果我直接用encode和aDecoder保存bool,bool每次都会返回nil。在互联网上找不到关于它的任何信息 正如你们在下面看到的,我只是用一个字符串来代替布尔,它就可以工作了 func boolwontsave(aBool:Bool) -> String { if aBool { retu

我是Swift(C++背景)的新手,我正在尝试做一件非常简单的事情:保存一个Bool。如果我将bool转换为“a”或“B”的字符串,然后再转换回来,那么效果会非常好。但是如果我直接用encode和aDecoder保存bool,bool每次都会返回nil。在互联网上找不到关于它的任何信息

正如你们在下面看到的,我只是用一个字符串来代替布尔,它就可以工作了

func boolwontsave(aBool:Bool) -> String {
    if aBool {
        return "A"
    }
    return "B"
}

func encode(with aCoder: NSCoder) {

    aCoder.encode(name, forKey: PropertyKey.name)
    aCoder.encode(number, forKey: PropertyKey.number)
    aCoder.encode(boolwontsave(aBool: ispresent), forKey: PropertyKey.present)

}

required convenience init?(coder aDecoder: NSCoder) {

    // The name is required. If we cannot decode a name string, the initializer should fail.
    guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
        os_log("Unable to decode the name for a player object.", log: OSLog.default, type: .debug)
        return nil
    }

    let number = aDecoder.decodeObject(forKey: PropertyKey.number) as? String

    guard let localpresent = aDecoder.decodeObject(forKey: PropertyKey.present) as? String else {
        print("got the nil")
        os_log("Unable to decode the ispresent for a player object.", log: OSLog.default, type: .debug)
        return nil
    }


    // Must call designated initializer.
    self.init(name:name, number:number, present: localpresent == "A")

}

布尔斯不应该救你吗?这似乎不雅

如果使用正确的API,保存
Bool
非常容易

NSCoding
a
Bool
不是对象而言,有一种
decodeBool(forKey
方法)

required convenience init?(coder aDecoder: NSCoder) {

    // The name is required. If we cannot decode a name string, the initializer should fail.
    guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else {
        os_log("Unable to decode the name for a player object.", log: OSLog.default, type: .debug)
        return nil
    }

    let number = aDecoder.decodeObject(forKey: PropertyKey.number) as? String
    let localpresent = aDecoder.decodeBool(forKey: PropertyKey.present)

    // Must call designated initializer.
    self.init(name:name, number:number, present: localpresent)

}

func encode(with aCoder: NSCoder) {
    aCoder.encode(name, forKey: PropertyKey.name)
    aCoder.encode(number, forKey: PropertyKey.number)
    aCoder.encode(ispresent, forKey: PropertyKey.present)
}
在Swift 4+中,我更喜欢本机的
Codable
协议,而不是受约束的
NSCoding

  • NSCoding
    需要继承自
    NSObject
  • Codable
    可用于符合协议的任何结构、类和枚举
使用decodeBool(forKey:String)代替decodeObject(forKey:String)

DecodeBool解码并返回一个布尔值,该布尔值以前是用encode(u:forKey:)编码并与字符串键关联的

aCoder.encode(true, forKey: PropertyKey.present)
aCoder.decodeBool(forKey: PropertyKey.present)

你说“拯救一个傻瓜”是什么意思。另外,请提供一个代码,其中您试图检索您的bool,它返回零。一个更大的问题是,为什么您在Swift中使用
NSCoding
NSCoder
。您不使用Swift
Codable
功能有什么原因吗?感谢所有的快速回答。我将转换为使用Codable,我认为是w我还要解决另一个关于保存嵌套数组的难题。再次感谢大家!