0

I have saved an array of custom objects that conforms to NSCoding protocol in UserDefaults using NSKeyedArchiver. But when I try to retrieve it using NSKeyedUnarchiver, i get runtime errors from Xcode. I have tried initialising NSArray using unarchived data but it also failed. My guess is that while unarchiving swift doesn't understand custom elements of this array. How shall I do it?

This is how I archived array of custom objects

static func saveCategoryList(_ categoryList : [Category]!) -> Void{
        let userDefaults = UserDefaults.standard
        let categoryListData = NSKeyedArchiver.archivedData(withRootObject: categoryList)
        userDefaults.set(categoryListData, forKey: Constants.CategoryList)
        userDefaults.synchronize()
    }

I get error like the attached screenshot in runtime. I am quite sure I am not doing it right. How can get my desired result? enter image description here

Rajamohan S
  • 7,229
  • 5
  • 36
  • 54
samin.yasar
  • 91
  • 1
  • 8

0 Answers0