Attempt to insert non-property list object when trying to save a custom object in Swift 3

You need to create Data instance from your JobCategory model using JSONEncoder and store that Data instance in UserDefaults and later decode using JSONDecoder. struct JobCategory: Codable { let id: Int let name: String } // To store in UserDefaults if let encoded = try? JSONEncoder().encode(category) { UserDefaults.standard.set(encoded, forKey: UserDefaultsKeys.jobCategory.rawValue) } // Retrieve from UserDefaults … Read more

Storing data to NSUserDefaults

You cannot use NSUserDefaults for a custom class. From the documentation: The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Booleans, and URLs. A default object must be a property list, that is, an instance of (or for collections a combination of instances of): NSData, NSString, NSNumber, NSDate, NSArray, … Read more

(Swift) Storing and retrieving Array to NSUserDefaults

From your code I see you are storing some array // Your code NSUserDefaults.standardUserDefaults().setObject(myArray, forKey: “\(identity.text!)listA”) and retrieving a string //Your code let tabledata = NSUserDefaults.standardUserDefaults().stringForKey(“\(identity.text!)listA”) There is probably a type mismatch, You store one type and retrieve another type. While retrieving either use arrayForKey() or objectForKey() see the code below. let tabledata = NSUserDefaults.standardUserDefaults().arrayForKey(“\(identity.text!)listA”) … Read more

How do I use UserDefaults with SwiftUI?

The approach from caram is in general ok but there are so many problems with the code that SmushyTaco did not get it work. Below you will find an “Out of the Box” working solution. 1. UserDefaults propertyWrapper import Foundation import Combine @propertyWrapper struct UserDefault<T> { let key: String let defaultValue: T init(_ key: String, … Read more

What’s the optimum way of storing an NSDate in NSUserDefaults?

You are needlessly complicating things. Why are you converting the date to a time interval (then the time interval to a different primitive)? Just [sharedDefaults setObject:theDate forKey:@”theDateKey”] and be done with it. NSDate is one of the “main types” supported by the PLIST format (dates, numbers, strings, data, dictionaries, and arrays), so you can just … Read more