0

在下面的代码中,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

任何帮助将不胜感激。

4

2 回答 2

2

这是因为您在方法中的全局变量 sharedinstance - (id)initWithCoder:(NSCoder *)aDecoder 始终为 nil

于 2013-03-21T17:03:09.440 回答
2

IninitWithCoder:不要指代共享实例,而是指代self。在它执行的时候sharedInstance是零。

此外,您只saveState在注销后调用,因此它只保存 nil 值。

于 2013-03-21T17:16:20.343 回答