3

我正在尝试将自定义对象保存到 NSUserDefaults 但它不起作用。这是代码。

/// 用户.m ////////////

#import "User.h"


@implementation User

....


+ (User *)getUser {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [defaults objectForKey:kUser];
User *obj = (User *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
return obj;

}


- (void)encodeWithCoder:(NSCoder *)encoder {
//Encode properties, other class variables, etc

 [encoder encodeObject:self.address1 forKey:@"address1"];
 [encoder encodeObject:self.address2 forKey:@"address2"];
 [encoder encodeObject:self.city forKey:@"city"];
 [encoder encodeObject:self.country forKey:@"country"];
 [encoder encodeObject:self.created_at forKey:@"created_at"];
 [encoder encodeObject:self.emergency_contact_name forKey:@"emergency_contact_name"];
 [encoder encodeObject:self.emergency_contact_phone_number   forKey:@"emergency_contact_phone_number"];
 [encoder encodeObject:self.facetime_email forKey:@"facetime_email"];
 [encoder encodeObject:self.gender forKey:@"gender"];
 [encoder encodeObject:self.password forKey:@"password"];
 [encoder encodeObject:self.phone_number forKey:@"phone_number"];
 [encoder encodeObject:self.photo_url forKey:@"photo_url"];
 [encoder encodeObject:self.role forKey:@"role"];
 [encoder encodeObject:self.security_answer forKey:@"security_answer"];
 [encoder encodeObject:self.security_question forKey:@"security_question"];
 [encoder encodeObject:self.state forKey:@"state"];
 [encoder encodeObject:self.updated_at forKey:@"updated_at"];
 [encoder encodeObject:self.userID forKey:@"userID"];
 [encoder encodeObject:self.username forKey:@"username"];
 [encoder encodeObject:self.zip forKey:@"zip"];
 [encoder encodeObject:self.email forKey:@"email"];

}

- (id)initWithCoder:(NSCoder *)decoder {

if((self = [super init])) {
    //decode properties, other class vars

    self.address1 = [decoder decodeObjectForKey:@"address1"];
    self.address2 = [decoder decodeObjectForKey:@"address2"];
    self.city = [decoder decodeObjectForKey:@"city"];
    self.country = [decoder decodeObjectForKey:@"country"];
    self.created_at = [decoder decodeObjectForKey:@"created_at"];
    self.emergency_contact_name = [decoder decodeObjectForKey:@"emergency_contact_name"];
    self.emergency_contact_phone_number = [decoder decodeObjectForKey:@"emergency_contact_phone_number"];
    self.facetime_email = [decoder decodeObjectForKey:@"facetime_email"];
    self.gender = [decoder decodeObjectForKey:@"gender"];
    self.password = [decoder decodeObjectForKey:@"password"];
    self.phone_number = [decoder decodeObjectForKey:@"phone_number"];
    self.photo_url = [decoder decodeObjectForKey:@"photo_url"];
    self.role = [decoder decodeObjectForKey:@"role"];
    self.security_answer = [decoder decodeObjectForKey:@"security_answer"];
    self.security_question = [decoder decodeObjectForKey:@"security_question"];
    self.state = [decoder decodeObjectForKey:@"state"];
    self.updated_at = [decoder decodeObjectForKey:@"updated_at"];
    self.userID = [decoder decodeObjectForKey:@"userID"];
    self.username = [decoder decodeObjectForKey:@"username"];
    self.zip = [decoder decodeObjectForKey:@"zip"];
    self.email = [decoder decodeObjectForKey:@"email"];
}
return self;
}


+ (void)saveUser:(User *)obj {
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myEncodedObject forKey:kUser];
[defaults synchronize];
}

+ (User *)loadUserWithKey:(NSString *)key {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [defaults objectForKey:key];
User *obj = (User *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
return obj;
}

+(void)deleteUser {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:nil forKey:kUser];

}


@end

/// 这是我保存用户的方法

  User *currentUser = [User getUser];
  currentUser.firstname = self.firstNameField.text;
  currentUser.lastname = self.lastNameField.text;
  currentUser.email = self.emailField.text;
  currentUser.password = self.passwordField.text;
  currentUser.phone_number = self.mobileField.text;

  [User saveUser:currentUser];

/// 这是我获取它的方式,但它返回时没有我在保存期间设置的任何属性集?怎么回事?

// Fill the textfields and image with the data
  User *currentUser = [User loadUserWithKey:kUser];
  self.firstNameField.text = currentUser.firstname;
  self.lastNameField.text = currentUser.lastname;
  self.mobileField.text = currentUser.phone_number;
  self.emailField.text = currentUser.email;

所有当前用户。东西是零;

4

0 回答 0