尝试将非属性列表对象设置为NSUserDefault我以为我知道是什么导致了这个错误,但我似乎不知道我做错了什么。下面是我得到的完整错误消息:Attempt to set a non-property-list object (
"<BC_Person: 0x8f3c140>"
) as an NSUserDefaults value for key personDataArray我有一个Person类,我认为该类符合NSCoding协议,在我的Person类中有这两种方法:- (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类,我认为该类正在处理BC_PersonClass..下面是我从DataSave班级:- (void)savePersonArrayData:(BC_Person *)personObject{
// NSLog(@"name of the person %@", personObject.personsName);
[mutableDataArray addObject:personObject];
// set the temp array to the mutableData array
tempMuteArray = [NSMutableArray arrayWithArray:mutableDataArray];
// save the person object as nsData
NSData *personEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:personObject];
// first add the person object to the mutable array
[tempMuteArray addObject:personEncodedObject];
// NSLog(@"Objects in the array %lu", (unsigned long)mutableDataArray.count);
// now we set that data array to the mutable array for saving
dataArray = [[NSArray alloc] initWithArray:mutableDataArray];
//dataArray = [NSArray arrayWithArray:mutableDataArray];
// save the object to NS User Defaults
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
[userData setObject:dataArray forKey:@"personDataArray"];
[userData synchronize];}我希望这是足够的代码给你一个想法,我想做什么。同样,我知道我的问题在于如何在BC_Person类中编码我的属性,我只是不知道自己做错了什么。谢谢你的帮助!
3 回答
蓝山帝景
TA贡献1843条经验 获得超7个赞
NSUserDefaults
NSCoding
NSArray
, NSDictionary
, NSString
, NSData
, NSNumber
NSDate
NSUserDefaults
.
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"];}
慕少森
TA贡献2019条经验 获得超9个赞
BC_Person is a non-property-list object
编辑NSUserDefaults
//: Playground - noun: a place where people can playimport Foundationclass 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 NSDatafunc archivePeople(people:[Person]) -> NSData { let archivedObject = NSKeyedArchiver.archivedDataWithRootObject(people as NSArray) return archivedObject}//: ### Create some peoplelet people = [Person(firstname: "johnny", surname:"appleseed"),Person(firstname: "peter", surname: "mill")]//: ### Archive our people to NSDatalet 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 NSUserDefaultslet 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")}
慕沐林林
TA贡献2016条经验 获得超9个赞
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];NSData *data = [NSKeyedArchiver archivedDataWithRootObject:yourObject]; [currentDefaults setObject:data forKey:@"yourKeyName"];
NSData *data = [currentDefaults objectForKey:@"yourKeyName"];yourObjectType * token = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[currentDefaults removeObjectForKey:@"yourKeyName"];
- 3 回答
- 0 关注
- 511 浏览
添加回答
举报
0/150
提交
取消