NSCoder没有正确返回值

时间:2013-03-21 16:42:19

标签: ios nscoding

在下面的代码中,在sharedInstance方法中永远不会正确设置属性,我无法弄清楚原因。在我使用归档程序保存之前,似乎我有正确的值。

  #import "User.h"

  static User *sharedInstance = nil;

  #define NAME @"name"
  #define USER_ID @"id"
  #define ACCOUNT_ID @"account_id"
  #define USER_NAME @"username"
  #define ADMIN @"admin"
  #define CURRENT_USER @"current_user"

  @implementation KSUser

  + (id)sharedInstance
  {
      @synchronized(self) {
          if (sharedInstance == nil) {
              NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:CURRENT_USER];
              if (userData) {
                  sharedInstance = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
              }
              else {
                  sharedInstance = [[super alloc] init];
              }
          }
      }
      return sharedInstance;
  }

  - (void)populateFromJSON:(NSDictionary *)json
  {
      sharedInstance.name = json[NAME];
      sharedInstance.accountId = json[ACCOUNT_ID];
      sharedInstance.userId = json[USER_ID];
      sharedInstance.userName = json[USER_NAME];
      sharedInstance.admin = [json[ADMIN] boolValue];
      sharedInstance.loggedIn = YES;
      NSLog(@"values are: name: %@, %@, %@, %@", sharedInstance.name, sharedInstance.accountId, sharedInstance.userId, sharedInstance.userName);
  }

  - (void)logout
  {
      sharedInstance.name = nil;
      sharedInstance.accountId = nil;
      sharedInstance.userId = nil;
      sharedInstance.userName = nil;
      sharedInstance.admin = NO;
      sharedInstance.loggedIn = NO;
      [self saveState];

  }

  - (void)saveState
  {
      NSLog(@"values are: name: %@, %@, %@, %@", sharedInstance.name, sharedInstance.accountId, sharedInstance.userId, sharedInstance.userName);
      NSData *data = [NSKeyedArchiver archivedDataWithRootObject:sharedInstance];
      [[NSUserDefaults standardUserDefaults] setObject:data forKey:CURRENT_USER];
  }

  - (void)encodeWithCoder:(NSCoder *)aCoder
  {
      [aCoder encodeObject:sharedInstance.userId forKey:USER_ID];
      [aCoder encodeObject:sharedInstance.accountId forKey:ACCOUNT_ID];
      [aCoder encodeObject:sharedInstance.name forKey:NAME];
      [aCoder encodeObject:sharedInstance.userName forKey:USER_NAME];
      [aCoder encodeBool:sharedInstance.admin forKey:ADMIN];
  }

  - (id)initWithCoder:(NSCoder *)aDecoder
  {
      if (self = [super init]) {
          sharedInstance.userId = [aDecoder decodeObjectForKey:USER_ID];
          sharedInstance.accountId = [aDecoder decodeObjectForKey:ACCOUNT_ID];
          sharedInstance.name = [aDecoder decodeObjectForKey:NAME];
          sharedInstance.userName = [aDecoder decodeObjectForKey:USER_NAME];
          sharedInstance.admin = [aDecoder decodeBoolForKey:ADMIN];
      }
      return self;
  }

  @end

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

这是因为你的全局变量sharedinstance in method - (id)initWithCoder:(NSCoder *)aDecoder总是为零

答案 1 :(得分:2)

initWithCoder:中,不要引用共享实例,而是引用self。当它正在执行sharedInstance时为零。

此外,您只需在退出后调用saveState,因此它只会保存零值。