我是swift的新手,并且正在尝试使用Apple"应用程序开发与Swift"来自学数据持久性。我订了。我试图创建并存储一系列学生,然后我可以回忆起。问题是,当我尝试重新加载它们时,数组是空的。我真的无法理解错误,因为我没有错误,只是一个空数组。 This is what I see when I run the code. 有人可以告诉我我做错了什么吗?任何帮助表示赞赏,但可能需要为我愚蠢。欢呼声。
import Foundation
class Student: NSObject, NSCoding {
var idNumber: String
var forename: String
var surname: String
var homeAddress: String
var postcode: String
var homePhone: String
var gender: String
var tutorGroup: String
var schoolEmail: String
var dateOfBirth: String
// property key strings are used for en and de coding later
struct PropertyKey {
static let idNumber = "idNumber"
static let forename = "forename"
static let surname = "surname"
static let homeAddress = "homeAddress"
static let postcode = "postcode"
static let homePhone = "homePhone"
static let gender = "gender"
static let tutorGroup = "tutorGroup"
static let schoolEmail = "schoolEmail"
static let dateOfBirth = "dateOfBirth"
}
static let DocumentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
static let ArchiveURL = DocumentsDirectory.appendingPathComponent("students")
init(idNumber: String, forename: String, surname: String,
homeAddress: String, postcode: String, homePhone: String,
gender: String, tutorGroup: String, schoolEmail: String,
dateOfBirth: String) {
self.idNumber = idNumber
self.forename = forename
self.surname = surname
self.homeAddress = homeAddress
self.postcode = postcode
self.homePhone = homePhone
self.gender = gender
self.tutorGroup = tutorGroup
self.schoolEmail = schoolEmail
self.dateOfBirth = dateOfBirth
}
static func loadFromFile() -> [Student] {
return NSKeyedUnarchiver.unarchiveObject(withFile: Student.ArchiveURL.path) as! [Student]
}
static func loadSampleStudents() -> [Student] {
return [
Student(idNumber: "13LDL", forename: "Dean", surname: "Lobo", homeAddress: "Neverland", postcode: "Far away", homePhone: "11111111", gender: "male", tutorGroup: "11L", schoolEmail: "13Lobo.d@gmail.com", dateOfBirth: "02/03/04"),
Student(idNumber: "14LSD", forename: "Harry", surname: "Harrison", homeAddress: "21 Bushey Mill Lane", postcode: "WD23 2DD", homePhone: "01923218324", gender: "male", tutorGroup: "11L", schoolEmail: "14Harrison.h@gmail.com", dateOfBirth: "02/03/05"),
Student(idNumber: "11LSA", forename: "Stefan", surname: "Stephens", homeAddress: "Somewhere near school", postcode: "SA12 2WP", homePhone: "01923123123", gender: "male", tutorGroup: "11L", schoolEmail: "11Adamovic.a@gmail.com", dateOfBirth: "08/03/01"),
Student(idNumber: "11LPJ", forename: "Penny", surname: "Pennyworth", homeAddress: "12 Downing Street", postcode: "W1 12AP", homePhone: "0192311111", gender: "female", tutorGroup: "11L", schoolEmail: "11Jane.P@gmail.com", dateOfBirth: "08/03/02"),
]
}
static func saveToFile(students: [Student]) {
NSKeyedArchiver.archiveRootObject(students, toFile: Student.ArchiveURL.path)
}
required convenience init?(coder aDecoder: NSCoder) {
guard let idNumber = aDecoder.decodeObject(forKey: PropertyKey.idNumber) as? String,
let forename = aDecoder.decodeObject(forKey: PropertyKey.forename) as? String,
let surname = aDecoder.decodeObject(forKey: PropertyKey.surname) as? String,
let homeAddress = aDecoder.decodeObject(forKey: PropertyKey.homeAddress) as? String,
let postcode = aDecoder.decodeObject(forKey: PropertyKey.postcode) as? String,
let homePhone = aDecoder.decodeObject(forKey: PropertyKey.homePhone) as? String,
let gender = aDecoder.decodeObject(forKey: PropertyKey.gender) as? String,
let tutorGroup = aDecoder.decodeObject(forKey: PropertyKey.tutorGroup) as? String,
let schoolEmail = aDecoder.decodeObject(forKey: PropertyKey.schoolEmail) as? String,
let dateOfBirth = aDecoder.decodeObject(forKey: PropertyKey.dateOfBirth) as? String
else {
return nil
}
self.init(idNumber: idNumber,
forename: forename,
surname: surname,
homeAddress: homeAddress,
postcode: postcode,
homePhone: homePhone,
gender: gender,
tutorGroup: tutorGroup,
schoolEmail: schoolEmail,
dateOfBirth: dateOfBirth
)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(idNumber, forKey: PropertyKey.idNumber)
aCoder.encode(forename, forKey: PropertyKey.forename)
aCoder.encode(surname, forKey: PropertyKey.surname)
aCoder.encode(homeAddress, forKey: PropertyKey.homeAddress)
aCoder.encode(postcode, forKey: PropertyKey.postcode)
aCoder.encode(homePhone, forKey: PropertyKey.homePhone)
aCoder.encode(gender, forKey: PropertyKey.gender)
aCoder.encode(tutorGroup, forKey: tutorGroup)
aCoder.encode(schoolEmail, forKey: PropertyKey.schoolEmail)
aCoder.encode(dateOfBirth, forKey: PropertyKey.dateOfBirth)
}
}
var students: [Student] = Student.loadSampleStudents()
print(students)
Student.saveToFile(students: students)
print("Student's were saved")
var reloadedStudents: [Student] = Student.loadFromFile()
print("student's were reloaded")
print(reloadedStudents)
答案 0 :(得分:0)
一个非常简单的错误,一个简单的修复。此行aCoder.encode(tutorGroup, forKey: tutorGroup)
需要替换为:
aCoder.encode(tutorGroup, forKey: PropertyKey.tutorGroup)
所以看起来应该是这样的:
func encode(with aCoder: NSCoder) {
aCoder.encode(idNumber, forKey: PropertyKey.idNumber)
aCoder.encode(forename, forKey: PropertyKey.forename)
aCoder.encode(surname, forKey: PropertyKey.surname)
aCoder.encode(homeAddress, forKey: PropertyKey.homeAddress)
aCoder.encode(postcode, forKey: PropertyKey.postcode)
aCoder.encode(homePhone, forKey: PropertyKey.homePhone)
aCoder.encode(gender, forKey: PropertyKey.gender)
aCoder.encode(tutorGroup, forKey: PropertyKey.tutorGroup)
aCoder.encode(schoolEmail, forKey: PropertyKey.schoolEmail)
aCoder.encode(dateOfBirth, forKey: PropertyKey.dateOfBirth)
}
我刚刚通过设置断点(在guard else
闭包内)发现了这一点。