这是我试图用nsencoding编码的自定义类的代码。变量Currensless不会初始化。
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)
}
}
现在,这是我尝试更改Curterless变量的代码:
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)
但是,正如我可以从打印语句(0/n 1/n 0(中看到的那样,Curressless var不会更新。为什么是这样?我很困惑如何更改它并保存它而不是初始化值之一。
您编码currentLesson
,但您不解码。您没有尝试在init(coder:)
初始化器中获取编码值。因此,当您调用其他init
时,它将重置回0
。
您需要更新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)
}