Store Custom Objects in NSUserDefaults

放肆的年华 提交于 2020-02-25 23:06:25

问题


I am trying to store a custom objects as follows, but I am getting an error.

// store data
NSMutableArray *archiveArray = [NSMutableArray arrayWithCapacity:pOrderElements.count];
for (id orderObject in pOrderElements) {
    NSData *personEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:orderObject];
    [archiveArray addObject:personEncodedObject];
}

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


// retrive data
NSData *data = [userData objectForKey:@"personDataArray"];
// the following line throws an error
id retrieveData = [NSKeyedUnarchiver unarchiveObjectWithData:data];

here is my pOrderElements

(lldb) po pOrderElements
<__NSArrayM 0x174256b90>(
{
    category = 1;
    imagePath = "products/chicken-kabob.png";
    name = "Baked Chicken";
    price = "5.49";
}
)

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray bytes]: unrecognized selector sent to instance 0x174264f40'


回答1:


You unarchive incorrect object. You store NSArray to NSUserDefaults

NSArray * archiveArray = [userData objectForKey:@"personDataArray"];
for (NSData *personEncodedObject in archiveArray) {
    id personObject = [NSKeyedUnarchiver unarchiveObjectWithData:personEncodedObject];
}



回答2:


First you create custom class Like below.

CustomObject.h

#import <Foundation/Foundation.h>

@interface CustomObject : NSObject

@property(strong,nonatomic)NSString *name;

@end 

CustomObject.m

#import "CustomObject.h"

@implementation CustomObject



- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.name forKey:@"name"];

}

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.name = [decoder decodeObjectForKey:@"name"];

    }
    return self;
}

Then create CustomObject class object and store in NSUserDefaults

Stored your object like this

CustomObject *object =[CustomObject new];
object.name = @"test";

NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr  addObject:object];

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:arr];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:encodedObject forKey:@"storeObject"];
[defaults synchronize];

get custom object from NSUserDefaults like this

NSData *storedEncodedObject = [defaults objectForKey:@"storeObject"];
    NSArray *arrStoreObject = [NSKeyedUnarchiver unarchiveObjectWithData:storedEncodedObject];
    for (int i=0; i<arrStoreObject.count; i++)
    {
        CustomObject *storedObject = [arrStoreObject objectAtIndex:i];
        NSLog(@"%@",storedObject.name);

    }


来源:https://stackoverflow.com/questions/43222681/store-custom-objects-in-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!