为什么在进行NSCoding

时间:2017-06-02 02:37:12

标签: swift nscoding

这是我尝试使用NSEncoding编码的自定义类的代码。变量currentLesson未初始化。

class UserData: NSObject, NSCoding {
    var race: [String]
    var religion: [String]
    var sexualOrientation: [String]
    var gender: [String]

    var currentLesson: Int

    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("user")

    struct PropertyKey {
        static let nameKey = "name"
        static let race = "race"
        static let religion = "religion"
        static let sexualOrientation = "sexualOrientation"
        static let gender = "gender"

        static let currentLesson = "currentLesson"
    }

    init?(raceArray: [String], religionArray: [String], sexualOrientationArray: [String], genderArray: [String]) {

        self.race = raceArray
        self.religion = religionArray
        self.sexualOrientation = sexualOrientationArray
        self.gender = genderArray

        self.currentLesson = 0

        super.init()
    }

    // NSCoding

    func encode(with aCoder: NSCoder) {
        aCoder.encode(race, forKey: PropertyKey.race)
        aCoder.encode(religion, forKey: PropertyKey.religion)
        aCoder.encode(sexualOrientation, forKey: PropertyKey.sexualOrientation)
        aCoder.encode(gender, forKey: PropertyKey.gender)

        aCoder.encode(currentLesson, forKey: PropertyKey.currentLesson)
    }

    required convenience init?(coder aDecoder: NSCoder) {
        let race = aDecoder.decodeObject(forKey: PropertyKey.race) as! [String]
        let religion = aDecoder.decodeObject(forKey: PropertyKey.religion) as! [String]
        let sexualOrientation = aDecoder.decodeObject(forKey: PropertyKey.sexualOrientation) as! [String]
        let gender = aDecoder.decodeObject(forKey: PropertyKey.gender) as! [String]

        self.init(raceArray: race, religionArray: religion, sexualOrientationArray: sexualOrientation, genderArray: gender)
    }
}

现在,这是我尝试更改currentLesson变量的代码:

let user = NSKeyedUnarchiver.unarchiveObject(withFile: UserData.ArchiveURL.path) as? UserData
print(user!.currentLesson)

user?.currentLesson = 1
print(user!.currentLesson)

NSKeyedArchiver.archiveRootObject(user!, toFile: UserData.ArchiveURL.path)

let user2 = NSKeyedUnarchiver.unarchiveObject(withFile: UserData.ArchiveURL.path) as? UserData
print(user2!.currentLesson)

但是,正如我从print语句(0 / n 1 / n 0)所知,currentLesson var不会更新。为什么是这样?我很困惑如何改变它并保存它而不是它是一个初始值。

1 个答案:

答案 0 :(得分:0)

您对currentLesson进行了编码,但您没有对其进行解码。您不会尝试在init(coder:)初始值设定项中获取编码值。因此,当您调用另一个0时,它会重置为init

您需要更新init方法。以下内容应该有效(请注意新currentLesson参数的默认值):

init?(raceArray: [String], religionArray: [String], sexualOrientationArray: [String], genderArray: [String], currentLesson: Int = 0) {
    self.race = raceArray
    self.religion = religionArray
    self.sexualOrientation = sexualOrientationArray
    self.gender = genderArray

    self.currentLesson = currentLesson

    super.init()
}

required convenience init?(coder aDecoder: NSCoder) {
    let race = aDecoder.decodeObject(forKey: PropertyKey.race) as! [String]
    let religion = aDecoder.decodeObject(forKey: PropertyKey.religion) as! [String]
    let sexualOrientation = aDecoder.decodeObject(forKey: PropertyKey.sexualOrientation) as! [String]
    let gender = aDecoder.decodeObject(forKey: PropertyKey.gender) as! [String]
    let currentLesson = aDecoder.decodeInteger(forKey: PropertyKey.currentLesson)

    self.init(raceArray: race, religionArray: religion, sexualOrientationArray: sexualOrientation, genderArray: gender, currentLesson: currentLesson)
}