2012-01-12 6 views
0

私は本当に奇妙な状況に陥っています。 isChecked 『:「私は名前のBOOL変数を使用してNSUserDefaultsに保存されている特定の条件をご確認のinitメソッドではシングルトンオブジェクトのWierdの動作

static Profile *currentProfile; 

+ (Profile *)currentProfile 
{ 
@synchronized(self) 
{ 
    if (currentProfile == nil) 
     currentProfile = [[Profile alloc] init]; 
} 

return currentProfile; 
} 

- (id)init 
{ 
self = [super init]; 
if (self) 
{ 
    // Initialization code here. 
    isChecked = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isChecked"] boolValue];  

    if (isChecked) 
    { 
     NSLog(@"isChecked block is called"); 
     NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"myEncodedObjectKey"]; 
     self   = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
     [self retain]; 

     for (int i = 0; i < self.avatar.count; i++) 
      [self.avatar replaceObjectAtIndex:i withObject:[UIImage imageWithData:[self.avatar objectAtIndex:i]]]; 
    } 

    else 
    { 
     password = @""; 
     pfefferID = @""; 
     email  = @""; 
     avatar = [[NSMutableArray alloc] initWithObjects: 
        [UIImage imageNamed:@"empty_image.png"], 
        [UIImage imageNamed:@"empty_image.png"], 
        [UIImage imageNamed:@"empty_image.png"], 
        [UIImage imageNamed:@"empty_image.png"], 
        [UIImage imageNamed:@"empty_image.png"],nil]; 
     isBoy  = YES; 
     friends = [[NSMutableDictionary alloc] init]; 
     rating = 0; 
    } 
    } 

return self; 
} 

このようなプロファイル』にisCheckedがYESに等しく、すべてが正常に動作します私は名前のクラスのsingletoneオブジェクトを作成します。 。しかし...私が使用しているときにモーメントが(今よりもはるかに少ない、実際に)前にYESに等しかったと同じにisChecked変数がNOに等しい取得します。このよう

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
users       = [[NSMutableDictionary alloc] init]; 
usersForLeaderboardFromServer = [[NSMutableDictionary alloc] init]; 
listOfFriendsFromServer  = [[NSMutableDictionary alloc] init]; 
currentProfile    = [Profile currentProfile]; 
sessionID      = 0; 

if (!currentProfile.isChecked)//why???? 
    NSLog(@"not checked"); 

if (currentProfile.isChecked) 
{ 
    [self getUsersForLeaderboardFromServer]; 

    MainMenuView *mainMenu = [[[MainMenuView alloc] init] autorelease]; 
    [self.navigationController pushViewController:mainMenu animated:YES]; 
} 
} 

ファイルAppDelegateで、このプロファイルオブジェクトを作成しますアプリケーションでは、ドットでアクセスしてFinishedLinunchingWithOptionsメソッドを実行しました。何が起こっていますか?私はそれを処理することができますが、私はちょうど好奇心が強いですこの状況。何が間違っているか知っていますか?

答えて

2

selfをinitに再割り当てしているため、isCheckedを設定したのではなく新しいオブジェクトが返されます。このコードを参照してください:

self   = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
[self retain]; 

あなたの持っているようなことをするのは少し面倒です - 私は確かにあなたが持っている方法で置き換えることをお勧めしません。最初に、currentProfileのスタティックに設定した値は、selfを割り当てなおすと更新されません。また、再割り当てするときに古いselfを解放していません。

id newSelf   = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
newSelf.isChecked = isChecked; 
[self release]; 
self = [newSelf retain]; 

しかし、私は本当に個人的にそれを提唱していない:あなたはこのような何かを行うことができ、それを修正するには

。私はあなたのアーカイブからオブジェクトにロードして、自己に再割り当てするのではなく、自分自身を更新することをお勧めします。

+0

ああ愚かな私、それは明らかです...ありがとう.. –

関連する問題