NSKeyedArchiver unarchiveObjectWithData返回值为nil的对象

时间:2018-08-07 13:08:59

标签: ios objective-c nsuserdefaults nskeyedarchiver nskeyedunarchiver

UserBean .h文件

@property (assign, readwrite) NSString *user;
@property (assign, readwrite) NSString *timestamp;
@property (assign, readwrite) NSString *password;
@property (assign, readwrite) NSString *source_id;
@property (assign, readwrite) NSString *fullname;
@property (assign, readwrite) NSString *email;
@property (assign, readwrite) NSString *reset_code;

- (void)encodeWithCoder:(NSCoder *)encoder;
- (id)initWithCoder:(NSCoder *)decoder;

UserBean .m文件

- (void)encodeWithCoder:(NSCoder *)encoder {
    //Encode properties, other class variables, etc
    [encoder encodeObject:self.email forKey:@"email"];
    [encoder encodeObject:self.user forKey:@"username"];
    [encoder encodeObject:self.fullname forKey:@"fullname"];
    [encoder encodeObject:self.password forKey:@"password"];
    [encoder encodeObject:self.source_id forKey:@"id"];
    [encoder encodeObject:self.timestamp forKey:@"timestamp"];
}
- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {
        //decode properties, other class vars
        self.email = [decoder decodeObjectForKey:@"email"];
        self.user = [decoder decodeObjectForKey:@"username"];
        self.fullname = [decoder decodeObjectForKey:@"fullname"];
        self.password = [decoder decodeObjectForKey:@"password"];
        self.source_id = [decoder decodeObjectForKey:@"id"];
        self.timestamp = [decoder decodeObjectForKey:@"timestamp"];
    }
    return self;
}

这里我要保存并找回userbean类对象

- (UserBean *)getUser                       {
    NSError *error;
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *encodedObject = [defaults objectForKey:@"user"];
    if (@available(iOS 9.0, *)) {
        UserBean *userBean = [NSKeyedUnarchiver unarchiveTopLevelObjectWithData:encodedObject error:&error];
        return userBean;
    } else {
        UserBean *userBean = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
        return userBean;
    }
}
- (void)saveUser:(UserBean *)userBean       {
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:userBean];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:encodedObject forKey:@"user"];
    [defaults synchronize];
}

从这里我正在设置对象属性并保存

- (void)saveUser:(StandardPacket *)packet               {
    NSDictionary *dataDict = [packet.data objectAtIndex:0];
    UserBean *userBean = [[UserBean alloc] init];

    userBean.user = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"username"]];
    userBean.email = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"email"]];
    userBean.fullname = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"fullname"]];
    userBean.password = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"password"]];
    userBean.source_id = [NSString stringWithFormat:@"%@",[dataDict objectForKey:@"id"]];

    userBean.timestamp = @"AA";
    userBean.reset_code = @"AA";

    [[SharedPrefences defaults] saveUser:userBean];
}

但是当我从SharedPreferences中检索对象时,我得到的对象的值为零和其他垃圾

线程1:EXC_BAD_ACCESS(代码= 1,地址= 0xa17e4c52)

检索后来自userbean对象的

用户UserBean * 0x17e604e0 _user NSString * 0x17ea9fe0

_timestamp NSString * 0x17ea7a50

_password NSString * 0x17e605c0

_source_id NSString * 0x17e679d0

_fullname NSString * 0x17ea9e50

_email NSString * 0x17e9a880

_reset_code NSString * nil 0x00000000

1 个答案:

答案 0 :(得分:0)

您缺少对encodeWithCoder:(NSCoder *)编码器函数进行编码的信息。

[encoder encodeObject:self.reset_code forKey:@"reset_code"];

此外,添加initWithCoder:(NSCoder *)解码器功能。

 self.reset_code = [decoder decodeObjectForKey:@"reset_code"];

希望有帮助。