我以为我知道造成这个错误的原因,但我似乎无法弄清楚我做错了什么。
以下是我收到的完整错误消息:
Attempt to set a non-property-list object ( "<BC_Person: 0x8f3c140>" ) as an NSUserDefaults value for key personDataArray
我有一个Person
类,我认为它符合NSCoding
协议,我在这个类中有这两种方法:
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:self.personsName forKey:@"BCPersonsName"];
[coder encodeObject:self.personsBills forKey:@"BCPersonsBillsArray"];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
self.personsName = [coder decodeObjectForKey:@"BCPersonsName"];
self.personsBills = [coder decodeObjectForKey:@"BCPersonsBillsArray"];
}
return self;
}
在应用中的某个时刻,NSString
中的BC_PersonClass
已设置,我有一个DataSave
类我认为正在处理我{{1}中的属性编码}}。
以下是我在BC_PersonClass
类中使用的代码:
DataSave
我希望这是足够的代码,让你知道我想做什么。 我再次知道我的问题在于如何在BC_Person类中编码我的属性,我似乎无法弄清楚我做错了什么。
感谢您的帮助!
答案 0 :(得分:257)
您发布的代码尝试将一组自定义对象保存到NSUserDefaults
。你不能这样做。实现NSCoding
方法没有帮助。您只能在NSArray
中存储NSDictionary
,NSString
,NSData
,NSNumber
,NSDate
和NSUserDefaults
等内容。< / p>
您需要将对象转换为NSData
(就像您在某些代码中所拥有的那样)并将NSData
存储在NSUserDefaults
中。如果需要,您甚至可以存储NSArray
NSData
。
当您回读数组时,您需要取消归档NSData
以取回BC_Person
个对象。
也许你想要这个:
- (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"];
}
答案 1 :(得分:65)
对我来说,运行数组并将对象自己编码为NSData似乎相当浪费。您的错误BC_Person is a non-property-list object
告诉您框架不知道如何序列化您的人物对象。
所以需要的是确保你的person对象符合NSCoding,然后你可以简单地将你的自定义对象数组转换为NSData并将其存储到默认值。这是一个操场:
编辑:在Xcode 7上写入NSUserDefaults
已被破坏,因此操场将存档到数据并返回并打印输出。 UserDefaults步骤包含在以后固定的情况下
//: Playground - noun: a place where people can play
import Foundation
class Person: NSObject, NSCoding {
let surname: String
let firstname: String
required init(firstname:String, surname:String) {
self.firstname = firstname
self.surname = surname
super.init()
}
//MARK: - NSCoding -
required init(coder aDecoder: NSCoder) {
surname = aDecoder.decodeObjectForKey("surname") as! String
firstname = aDecoder.decodeObjectForKey("firstname") as! String
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(firstname, forKey: "firstname")
aCoder.encodeObject(surname, forKey: "surname")
}
}
//: ### Now lets define a function to convert our array to NSData
func archivePeople(people:[Person]) -> NSData {
let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(people as NSArray)
return archivedObject
}
//: ### Create some people
let people = [Person(firstname: "johnny", surname:"appleseed"),Person(firstname: "peter", surname: "mill")]
//: ### Archive our people to NSData
let peopleData = archivePeople(people)
if let unarchivedPeople = NSKeyedUnarchiver.unarchiveObjectWithData(peopleData) as? [Person] {
for person in unarchivedPeople {
print("\(person.firstname), you have been unarchived")
}
} else {
print("Failed to unarchive people")
}
//: ### Lets try use NSUserDefaults
let UserDefaultsPeopleKey = "peoplekey"
func savePeople(people:[Person]) {
let archivedObject = archivePeople(people)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(archivedObject, forKey: UserDefaultsPeopleKey)
defaults.synchronize()
}
func retrievePeople() -> [Person]? {
if let unarchivedObject = NSUserDefaults.standardUserDefaults().objectForKey(UserDefaultsPeopleKey) as? NSData {
return NSKeyedUnarchiver.unarchiveObjectWithData(unarchivedObject) as? [Person]
}
return nil
}
if let retrievedPeople = retrievePeople() {
for person in retrievedPeople {
print("\(person.firstname), you have been unarchived")
}
} else {
print("Writing to UserDefaults is still broken in playgrounds")
}
而且,Voila,您已将一系列自定义对象存储到NSUserDefaults
中答案 2 :(得分:47)
保存:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-striped basic">
<thead>
<tr>
<th>Saturday</th>
<th class="time">10:30am</th>
<th class="time">11:00am</th>
<th class="time">11:30am</th>
<th class="time">12:00pm</th>
</tr>
</thead>
<tbody>
<tr>
<td>3/5/2016</td>
<td>15231</td>
<td>15232</td>
<td>15233</td>
<td>15234</td>
</tr>
<tr>
<td>3/5/2016</td>
<td>15231</td>
<td>15232</td>
<td>15233</td>
<td>15234</td>
</tr>
</tbody>
</table>
<table class="table table-striped basic">
<thead>
<tr>
<th>Monday</th>
<th class="time">7:30pm</th>
<th class="time">9:00pm</th>
<th class="time">10:30pm</th>
</tr>
</thead>
<tbody>
<tr>
<td>3/7/2016</td>
<td>15231</td>
<td>15232</td>
<td>15233</td>
</tr>
<tr>
<td>3/7/2016</td>
<td>15231</td>
<td>15232</td>
<td>15233</td>
</tr>
</tbody>
</table>
获取:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourObject];
[currentDefaults setObject:data forKey:@"yourKeyName"];
删除
NSData *data = [currentDefaults objectForKey:@"yourKeyName"];
yourObjectType * token = [NSKeyedUnarchiver unarchiveObjectWithData:data];
答案 3 :(得分:31)
class ArchiveUtil {
private static let PeopleKey = "PeopleKey"
private static func archivePeople(people : [Human]) -> NSData {
return NSKeyedArchiver.archivedData(withRootObject: people as NSArray) as NSData
}
static func loadPeople() -> [Human]? {
if let unarchivedObject = UserDefaults.standard.object(forKey: PeopleKey) as? Data {
return NSKeyedUnarchiver.unarchiveObject(with: unarchivedObject as Data) as? [Human]
}
return nil
}
static func savePeople(people : [Human]?) {
let archivedObject = archivePeople(people: people!)
UserDefaults.standard.set(archivedObject, forKey: PeopleKey)
UserDefaults.standard.synchronize()
}
}
class Human: NSObject, NSCoding {
var name:String?
var age:Int?
required init(n:String, a:Int) {
name = n
age = a
}
required init(coder aDecoder: NSCoder) {
name = aDecoder.decodeObject(forKey: "name") as? String
age = aDecoder.decodeInteger(forKey: "age")
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(name, forKey: "name")
aCoder.encode(age, forKey: "age")
}
}
var people = [Human]()
people.append(Human(n: "Sazzad", a: 21))
people.append(Human(n: "Hissain", a: 22))
people.append(Human(n: "Khan", a: 23))
ArchiveUtil.savePeople(people: people)
let others = ArchiveUtil.loadPeople()
for human in others! {
print("name = \(human.name!), age = \(human.age!)")
}
答案 4 :(得分:10)
首先,rmaddy的答案(上图)是正确的:实施NSCoding
没有帮助。但是,您需要实施NSCoding
以使用NSKeyedArchiver
以及所有这些,因此只需再一步......通过NSData
进行转换。
示例方法
- (NSUserDefaults *) defaults {
return [NSUserDefaults standardUserDefaults];
}
- (void) persistObj:(id)value forKey:(NSString *)key {
[self.defaults setObject:value forKey:key];
[self.defaults synchronize];
}
- (void) persistObjAsData:(id)encodableObject forKey:(NSString *)key {
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:encodableObject];
[self persistObj:data forKey:key];
}
- (id) objectFromDataWithKey:(NSString*)key {
NSData *data = [self.defaults objectForKey:key];
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
因此,您可以将NSCoding
个对象包裹在NSArray
或NSDictionary
或其他任何内容中......
答案 5 :(得分:10)
试用此代码
您无法在NSUserDefault中存储映射器,只能存储NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。
let myData = NSKeyedArchiver.archivedData(withRootObject: myJson)
UserDefaults.standard.set(myData, forKey: "userJson")
let recovedUserJsonData = UserDefaults.standard.object(forKey: "userJson")
let recovedUserJson = NSKeyedUnarchiver.unarchiveObject(with: recovedUserJsonData)
答案 6 :(得分:6)
快速和@propertyWrapper
将Codable
对象保存到UserDefault
@propertyWrapper
struct UserDefault<T: Codable> {
let key: String
let defaultValue: T
init(_ key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
if let data = UserDefaults.standard.object(forKey: key) as? Data,
let user = try? JSONDecoder().decode(T.self, from: data) {
return user
}
return defaultValue
}
set {
if let encoded = try? JSONEncoder().encode(newValue) {
UserDefaults.standard.set(encoded, forKey: key)
}
}
}
}
enum GlobalSettings {
@UserDefault("user", defaultValue: User(name:"",pass:"")) static var user: User
}
示例用户模型确认可编码
struct User:Codable {
let name:String
let pass:String
}
使用方法
//Set value
GlobalSettings.user = User(name: "Ahmed", pass: "Ahmed")
//GetValue
print(GlobalSettings.user)
答案 7 :(得分:5)
尝试将字典保存到NSUserDefaults
时遇到此问题。事实证明它不会保存,因为它包含NSNull
个值。所以我只是将字典复制到一个可变字典中,删除了空值,然后保存到NSUserDefaults
NSMutableDictionary* dictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary_trying_to_save];
[dictionary removeObjectForKey:@"NullKey"];
[[NSUserDefaults standardUserDefaults] setObject:dictionary forKey:@"key"];
在这种情况下,我知道哪些键可能是NSNull
值。
答案 8 :(得分:3)
https://developer.apple.com/reference/foundation/userdefaults
默认对象必须是属性列表 - 即(或集合,实例的组合)的实例:NSData,NSString,NSNumber,NSDate,NSArray或NSDictionary。
如果要存储任何其他类型的对象,通常应将其存档以创建NSData实例。有关更多详细信息,请参阅“首选项和设置编程指南”。
答案 9 :(得分:1)
快捷键5:可以使用 Codable 协议代替 NSKeyedArchiever 。
struct User: Codable {
let id: String
let mail: String
let fullName: String
}
Pref 结构是围绕UserDefaults标准对象的自定义包装。
struct Pref {
static let keyUser = "Pref.User"
static var user: User? {
get {
if let data = UserDefaults.standard.object(forKey: keyUser) as? Data {
do {
return try JSONDecoder().decode(User.self, from: data)
} catch {
print("Error while decoding user data")
}
}
return nil
}
set {
if let newValue = newValue {
do {
let data = try JSONEncoder().encode(newValue)
UserDefaults.standard.set(data, forKey: keyUser)
} catch {
print("Error while encoding user data")
}
} else {
UserDefaults.standard.removeObject(forKey: keyUser)
}
}
}
}
因此您可以通过以下方式使用它:
Pref.user?.name = "John"
if let user = Pref.user {...
答案 10 :(得分:0)
Swift 5 非常简单的方法
//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"
答案 11 :(得分:0)
我遇到了这个问题,最终发现这是因为我试图使用NSNumber
作为字典键,而属性列表仅允许将字符串用作键。 setObject:forKey:
的文档中没有提到此限制,但是链接到的About Property Lists页面却做了:
按照惯例,表2-1中列出的每个Cocoa和Core Foundation对象都称为属性列表对象。从概念上讲,您可以将“属性列表”视为所有这些类的抽象超类。如果从某个方法或函数接收到一个属性列表对象,则知道它必须是这些类型之一的实例,但是先验地您可能不知道哪种类型。如果属性列表对象是容器(即数组或字典),则其中包含的所有对象也必须是属性列表对象。如果数组或字典包含的对象不是属性列表对象,则无法使用各种属性列表方法和函数来保存和还原数据的层次结构。尽管NSDictionary和CFDictionary对象允许它们的键成为任何类型的对象,但如果键不是字符串对象,则集合也不是属性列表对象。
(强调我的)