Xcode-project不使用NSKeyedArchiver进行存档,但是Playground执行

时间:2017-03-02 19:23:34

标签: swift xcode swift3 swift-playground

我在使用NSCoding和NSObject存档时遇到了一些问题。这在Playground完美无缺。但似乎在Xcode中不起作用。有人可以帮我解决这个问题吗?

每次我打电话:

NSKeyedArchiver.archiveRootObject(person, toFile: "person")

它在Playground中工作并返回true,但在项目中返回false。我不使用模拟器。

我一直在寻找相同类型的问题,但我似乎无法找到与此相关的任何内容。

class Person: NSObject, NSCoding {

private var _name: String = "name"
private var _birthdate: Date = Date()
private var _income: Int = -1
private var _wealth: Int = -1

var name: String {
    get { return _name }
    set (newName) { _name = newName }
}
var birthdate: Date {
    get { return _birthdate }
    set (newBirthdate) { _birthdate = newBirthdate }
}
var income: Int {
    get { return _income }
    set (newIncome) { _income = newIncome }
}
var wealth: Int {
    get { return _wealth }
    set (newWealth) { _wealth = newWealth }
}

override init(){}

required convenience init(coder unarchiver: NSCoder) {

    self.init()

    if let name = unarchiver.decodeObject(forKey: "name") as? String {
        self.name = name
    }
    if let birthdate = unarchiver.decodeObject(forKey: "birthdate") as? Date {
        self.birthdate = birthdate
    }
    let income = unarchiver.decodeInteger(forKey: "income")
    self.income = income

    let wealth = unarchiver.decodeInteger(forKey: "wealth")
    self.wealth = wealth

}

func encode(with archiver: NSCoder) {

    archiver.encode(name, forKey: "name")
    archiver.encode(birthdate, forKey: "birthdate")
    archiver.encode(income, forKey: "income")
    archiver.encode(wealth, forKey: "wealth")
} 
}

然后我创建一个人并将其存档。

let person = Person()
NSKeyedArchiver.archiveRootObject(person, toFile: "person")

1 个答案:

答案 0 :(得分:2)

您需要在Person类中对init进行一些小编辑。删除whpp_track_post_views关键字并将convenience替换为self.init()

super.init()

一旦你这样做,你可以在任何你想要的地方调用存档器(我在viewDidLoad中只是为了测试并且它工作了),给它一个这样的路径:

class Person: NSObject, NSCoding {

    private var _name: String = "name"
    private var _birthdate: Date = Date()
    private var _income: Int = -1
    private var _wealth: Int = -1

    var name: String {
        get { return _name }
        set (newName) { _name = newName }
    }
    var birthdate: Date {
        get { return _birthdate }
        set (newBirthdate) { _birthdate = newBirthdate }
    }
    var income: Int {
        get { return _income }
        set (newIncome) { _income = newIncome }
    }
    var wealth: Int {
        get { return _wealth }
        set (newWealth) { _wealth = newWealth }
    }

    override init() {}

    required init(coder unarchiver: NSCoder) {

        super.init()

        if let name = unarchiver.decodeObject(forKey: "name") as? String {
            self.name = name
        }
        if let birthdate = unarchiver.decodeObject(forKey: "birthdate") as? Date {
            self.birthdate = birthdate
        }
        let income = unarchiver.decodeInteger(forKey: "income")
        self.income = income

        let wealth = unarchiver.decodeInteger(forKey: "wealth")
        self.wealth = wealth

    }

    func encode(with archiver: NSCoder) {

        archiver.encode(name, forKey: "name")
        archiver.encode(birthdate, forKey: "birthdate")
        archiver.encode(income, forKey: "income")
        archiver.encode(wealth, forKey: "wealth")
    } 
}