I have a class called Stop. This class implements NSCoding. I can save instances of Stop to UserDefaults with no problems. So far, so good. My problem is with saving an Array of stops. This is what I try:
private func save(stopArray array: [Stop],withKey key: String) {
let data = NSKeyedArchiver.archivedData(withRootObject: array)
self.userDefaults.set(data, forKey: key)
self.userDefaults.synchronize()
}
private func loadStopArray(key: String) -> [Stop]? {
guard let data = self.userDefaults.object(forKey: key) as? Data else {
return nil
}
return NSKeyedUnarchiver.unarchiveObject(with: data) as? [Stop]
}
Now, every time I call loadStopArray I get an empty array. Not nil, just an empty array.
Any ideas? Thanks!