Attempt to set a non-property-list object as an NSUserDefaults

前端 未结 11 1631
闹比i
闹比i 2020-11-22 15:00

I thought I knew what was causing this error, but I can\'t seem to figure out what I did wrong.

Here is the full error message I am getting:

Attempt to se         


        
11条回答
  •  粉色の甜心
    2020-11-22 15:36

    Swift 5 Very Easy way

    //MARK:- First you need to encoded your arr or what ever object you want to save in UserDefaults
    //in my case i want to save Picture (NMutableArray) in the User Defaults in
    //in this array some objects are UIImage & Strings
    
    //first i have to encoded the NMutableArray 
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: yourArrayName)
    //MARK:- Array save in UserDefaults
    defaults.set(encodedData, forKey: "YourKeyName")
    
    //MARK:- When you want to retreive data from UserDefaults
    let decoded  = defaults.object(forKey: "YourKeyName") as! Data
    yourArrayName = NSKeyedUnarchiver.unarchiveObject(with: decoded) as! NSMutableArray
    
    //MARK: Enjoy this arrry "yourArrayName"
    

提交回复
热议问题