我有一个很奇怪的情况。我创建一个名为类的singletone对象“简介是这样的:单身物体的奇怪行为
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;
}
在init()方法我检查通过使用名为布尔变量存储在NSUserDefaults的一定条件下‘器isChecked’器isChecked等于YES,一切工作正常。但是...我创建的AppDelegate此档案文件对象这样
- (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];
}
}
所以同一变量器isChecked这一刻(远远低于实际的时刻)前等于YES变得等于NO使用时在应用程序didFinishLaunchingWithOptions方法通过点访问它。发生了什么?我能够处理它,但我只是很好奇阿博这种情况。你知道它有什么问题吗?
哦,我很傻,很明显...谢谢.. – 2012-01-12 15:12:34