0

This question is not the same as - Saving custom SWIFT class with NSCoding to UserDefaults and it is different because there are 2 classes that are connected to each other and I don't know to implement NSCoding for both of them.I am beginner and I have been trying for more than 6 hours to solve this problem.And btw this is not a duplicate because all the solutions on here are outdated. I have 2 main custom classes(super class and subclass) in my app and with UserDefaults I want to make so I can save data permanently.

The first class:

class Day { 
    var dayName: String
    var subjects: [Subject]?
    init(dayName: String) {
       self.dayName = dayName
    }
}

The second class:

class Subject: Day {
    var subjectName: String
    var startsAt: String?
    init(dayName: String,subjectName: String) {
       self.subjectName = subjectName
       super.init(dayName: dayName)
    }    
}

I try to save a instance of Subject class like this:

UserDefaults.standard.set(Subject(dayName: "Monday",subjectName: "Programming"), forKey: "subject")
    let savedSubject = UserDefaults.standard.object(forKey: "subject") as? Subject
    print(savedSubject!.subjectName)

And then when I run the app I get a crash saying -

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object School_Class_Scedule.Subject for key subject'

I did a lot of research and found out that I should convert the subject to NSData but I don't know how to do that with swift 3 or 4 in XCode 9. Any help would be much appreciated. Thank you for saving my life and helping me my dream to become a reality.

  • This **is** a duplicate. What's wrong with your [previous question](https://stackoverflow.com/questions/47610151/must-call-a-designated-initializer-of-the-superclass-day-error)? – vadian Dec 02 '17 at 22:23
  • 1
    Possible duplicate of [Saving custom SWIFT class with NSCoding to UserDefaults](https://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults) – vadian Dec 02 '17 at 22:26
  • Nothing is wrong just thought to I kind of solved that problem and then I got this problem and know I'm trying to solve it. Help me if you can – Blockchain Developer Dec 02 '17 at 22:31
  • 1
    It's mandatory to use `NSCoding` if you are going to save a custom class into `UserDefaults` unless all properties are property list or JSON compliant then you can use the appropriate serialization, too (in Swift 4 even `Codable`). – vadian Dec 02 '17 at 22:35
  • So I cannot save all the data like there Subject instances only with UserDefaults and without implementing NSCoding ? – Blockchain Developer Dec 02 '17 at 22:37
  • 1
    No, you cannot. Actually you can using the mentioned alternatives if the classes are JSON or property list compliant. – vadian Dec 02 '17 at 22:37
  • This is the last big problem that I have before I submit the app for the app store so can you write the code on how I can implement NSCoding in my code please and give me an example ? – Blockchain Developer Dec 02 '17 at 22:42
  • 1
    Are you serious? In my answer to your previous question the implementation of `NSCoding` was included. – vadian Dec 02 '17 at 22:44
  • Okay thank you I just realized so with that out of the way I can convert data to NSData as shown in this example(https://stackoverflow.com/questions/26469457/saving-custom-swift-class-with-nscoding-to-userdefaults) and finally save data permanently ? – Blockchain Developer Dec 02 '17 at 22:54

0 Answers0