0

In my app I have 2 custom classes (super class and subclass) and based on them data is created and deleted from the user dynamically and I want to be able to save the data permanently with UserDefaults and I'm not sure how.I tried looking the answers like this one - Saving custom SWIFT class with NSCoding to UserDefaults and I fint it hard to understand.

My first class(super class):

class Day { 
var dayName: String
var subjects: [Subject]?

init(dayName: String) {
    self.dayName = dayName
}

}

My second class(subclass):

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

}

Thanks for helping me.

Viktor
  • 75
  • 4
  • 13
  • I wouldn't suggest you use UserDefaults; it isn't really intended for storing large amounts of data using custom classes. You would need to have your classes implement the NSCoding protocols. You could look at CoreData instead. – Paulw11 Dec 07 '17 at 22:04
  • Where do I have the resources to learn core data? I tried to avoid that because as far as I know it is on an intermediate level and I am beginner. – Viktor Dec 07 '17 at 22:06
  • 2
    There are lots of tutorials; just search. By the time you mess around with NSCoding, core data is probably easier. For example you create the object descriptions in your data model and it will create the class definitions for you. – Paulw11 Dec 07 '17 at 22:09
  • So can I also use that if I didn't enable it when I was creating the project ? My app is pretty much done and this is the only thing left. – Viktor Dec 08 '17 at 09:51
  • It isn't hard. The simplest way is probably to create a new project with core data support and then copy the relevant code from the new appdelegate isn't your existing appdelegate. – Paulw11 Dec 08 '17 at 10:06
  • I did get that but I am not sure if I have core data in my app. Is having firebase connected with my app make any problems with coreData? – Viktor Dec 08 '17 at 10:42

0 Answers0