2017-09-17 41 views
1

私は素早く新しいです.Apple Development with Swiftを使ってデータの永続性を教えようとしていました。私は後で思い出すことのできる生徒の配列を作成して保存しようとしています。問題は、それらをリロードしようとすると、配列が空であることです。私はエラーがないので、何が間違っているのか分かりません。空の配列です。 This is what I see when I run the code. 私が間違っていることを誰かに教えてもらえますか?どんな助けもありがたいですが、それは私のために下手にする必要があるかもしれません。乾杯。Swift 3でデータを読み込むときに空の配列

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: "[email protected]", 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: "[email protected]", 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: "[email protected]", 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: "[email protected]", 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

いいえ、それはオブジェクトを参照してオブジェクトを参照してくださいので、オブジェクトが表示されますので、塗りつぶされた配列を返しますstudents.countを印刷しようとしました4 – Hosny

+1

@ Hosnyいいえそれは – matt

+0

の結果は何ですか? – Hosny

答えて

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クロージャ内の)ブレークポイントを設定することにより、これを発見しました。

+0

ありがとうございました – DeanLobo

関連する問題