NSKeyedArchiver unarchiveObjectWithData 返回具有 nil 值的对象

NSKeyedArchiver unarchiveObjectWithData returning object with nil values

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 class 对象

- (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 中检索对象时,我得到的对象具有 nil 值和其他垃圾

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

检索后来自 userbean 对象的值

用户UserBean * 0x17e604e0 _user NSString * 0x17ea9fe0

_timestamp NSString * 0x17ea7a50

_密码 NSString * 0x17e605c0

_source_id NSString * 0x17e679d0

_全名 NSString * 0x17ea9e50

_email NSString * 0x17e9a880

_reset_code NSString * 无 0x00000000

您缺少对 in encodeWithCoder:(NSCoder *)encoder 函数进行编码。

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

另外,加入initWithCoder:(NSCoder *)decoder函数

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

希望对您有所帮助。