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

initWithCoder:共有インスタンスを参照するのではなく、代わりに参照してくださいself。実行時sharedInstanceはゼロです。

saveStateまた、ログアウト後にのみ呼び出すため、nil 値のみが保存されます。

于 2013-03-21T17:16:20.343 に答える