我是swift的新手,我正在努力坚持数据。我创建了一个名为Students的类,其中存储了学生的基本属性,例如名称和计划对象数组。该计划是另一个包含每个计划实例的计数器的类。
class Students: NSObject, NSCoding{
var name: String = ""
var schedule = [Schedule]()
static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("students")
struct PropertyKey {
static let nameKey = "name"
static let scheduleKey = "schedule"
}
init(name: String, schedule: [Schedule]){
self.name = name
self.schedule = schedule
super.init()
}
init(name: String)
{
self.name = name
super.init()
}
func addClass(className: String){
let scheduleItem = Schedule()
scheduleItem.scheduleName = className
schedule.append(scheduleItem)
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name,forKey: PropertyKey.nameKey)
aCoder.encodeObject(schedule, forKey: PropertyKey.scheduleKey)
}
required convenience init?(coder aDecoder: NSCoder){
let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String
let schedule = aDecoder.decodeObjectForKey(PropertyKey.scheduleKey) as! [Schedule]
self.init(name: name, schedule: schedule)
}
}
class Schedule{
var counter = 0
var scheduleName: String = ""
func increase()
{
counter += 1
}
func reset()
{
counter = 0
}
}
我的问题是,当我调用我的保存方法时
func save(){
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(students, toFile: Students.ArchiveURL.path!)
if !isSuccessfulSave {
print("Failed to save meals...")
}
}
我从第
行收到错误aCoder.encodeObject(schedule, forKey: PropertyKey.scheduleKey)
为什么不让我保存计划对象数组?