Attempt to Set a Non-Property-List Object as an Nsuserdefaults

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

The code you posted tries to save an array of custom objects to NSUserDefaults. You can't do that. Implementing the NSCoding methods doesn't help. You can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults.

You need to convert the object to NSData (like you have in some of the code) and store that NSData in NSUserDefaults. You can even store an NSArray of NSData if you need to.

When you read back the array you need to unarchive the NSData to get back your BC_Person objects.

Perhaps you want this:

- (void)savePersonArrayData:(BC_Person *)personObject {
[mutableDataArray addObject:personObject];

NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:mutableDataArray.count];
for (BC_Person *personObject in mutableDataArray) {
NSData *personEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:personObject];
[archiveArray addObject:personEncodedObject];
}

NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:archiveArray forKey:@"personDataArray"];
}

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

You can't have null in there.

property lists do not support explicit nulls

Wikipedia

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 instance using archivedData(withRootObject:) and store that Data instance in UserDefaults and later unarchive using unarchiveTopLevelObjectWithData(_:), So try like this.

For Storing data in UserDefaults

let category = JobCategory(id: 1, name: "Test Category", URLString: "http://www.example-job.com")
let encodedData = NSKeyedArchiver.archivedData(withRootObject: category, requiringSecureCoding: false)
let userDefaults = UserDefaults.standard
userDefaults.set(encodedData, forKey: UserDefaultsKeys.jobCategory.rawValue)

For retrieving data from UserDefaults

let decoded  = UserDefaults.standard.object(forKey: UserDefaultsKeys.jobCategory.rawValue) as! Data
let decodedTeams = NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(decoded) as! JobCategory
print(decodedTeams.name)

UserDefaults save String - Attempt to set a non-property-list object (Function) as an NSUserDefaults/CFPreferences

toReadableString() is an instance method, you have to call it on timeResponse rather than on the type which returns the function the compiler is complaining about by the way.

let jsonTransferTime = timeResponse.toReadableString()

Please conform to the naming convention that variable names start with a lowercase letter and don't annotate types the compiler can infer.

NSUserDefaultsController: Attempt to set a non-property-list object ... as an NSUserDefaults/CFPreferences value for key ...

I solved the problem by subclassing NSArrayController as follows (see comment by Hamish to my other question, which was the last missing piece of the puzzle to make this generic):

extension Encodable {
fileprivate func encode(to container: inout SingleValueEncodingContainer) throws {
try container.encode(self)
}
}

struct AnyEncodable: Encodable {
var value: Encodable
init(_ value: Encodable) {
self.value = value
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try value.encode(to: &container)
}
}

class NSEncodableArrayController: NSArrayController {
override func addObject(_ object: Any) {
let data = try! PropertyListEncoder().encode(AnyEncodable(object as! Encodable))
let any = try! PropertyListSerialization.propertyList(from: data, options: [], format: nil)

super.addObject(any)
}
}

Userdefault error with Attempt to set a non-property-list object

Please read the error message carefully, it's pretty clear.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object ( "(temperature: \"C\", measurement: \"mm\")" ) for key Current Weather Setting'

The list object is a tuple which is not property list compliant.

A reasonable solution is a custom struct and Codable, the completion handler is pointless because UserDefaults is synchronous.

struct WeatherUnit : Codable {
let temperature, measurement : String
}

var currentWeatherUnit = [WeatherUnit]()

func checkingForSetting() {
if let data = UserDefaults.standard.data(forKey: "Current Weather Setting") {
do {
currentWeatherUnit = try PropertyListDecoder().decode([WeatherUnit].self, from: data)
print("Current weather setting is exist")
} catch { print(error) }
} else {
currentWeatherUnit = [WeatherUnit(temperature: "C", measurement: "mm")]
let data = try? PropertyListEncoder().encode(currentWeatherUnit)
UserDefaults.standard.set(data, forKey: "Current Weather Setting")
print("Create new setting for current weather")
}
}

iOS - Attempt to insert non-property list object NSDictionary in NSUserDefaults

Some of your objects or keys in dictionary have class, that doesn't support property list serialization. Please, see this answer for details: Property list supported ObjC types

I recommend to check object for key "profile_picture" - it may be NSURL.

If that doesn't help, you may use following code to identify incompatible object:

for (id key in userObjectDictionary) {
NSLog(@"key: %@, keyClass: %@, value: %@, valueClass: %@",
key, NSStringFromClass([key class]),
userObjectDictionary[key], NSStringFromClass([userObjectDictionary[key] class]));
}

Cannot set non-property-list object in UserDefaults

The answer was to use keyedArchiver to convert the object to an NSData object. Here is the Swift 3 code for storing, and retrieving the object:

func saveGame() {
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: hero), forKey: HeroObjectKey)
}

func defaultExistsForGameData() -> Bool {

var gameData = false

if let heroObject = UserDefaults.standard.value(forKey: HeroObjectKey) as? NSData {
hero = NSKeyedUnarchiver.unarchiveObject(with: heroObject as Data) as! Hero
gameData = true
}

return gameData
}


Related Topics



Leave a reply



Submit