2012-11-04 43 views
0

我在Objective-C程序中遇到了一個令人困惑的問題。我試圖在我的程序進入後臺時從NSMutableArray保存數據。我的AppDelegate中有一個名爲savedResults的靜態變量。視圖控制器在我的程序的生命週期中操作這個變量並向其添加數據。我有一個邏輯條件來檢查savedResults是否爲空,如果不是,那麼我需要保存數據。這裏是我的代碼:如果全局變量不是 - 目標C

NSString *const kFileName = @"PCFData.bin"; 
//these are all my static variables..I have to initialize them to something so 
//they can be used in other parts of my program with the keyword extern. 
NSString *finalTermValue = @""; 
NSString *finalClassValue = @""; 
NSString *finalTermDescription = @""; 
NSMutableArray *savedResults = nil; 

@implementation PCFAppDelegate 

@synthesize finalTermValue, finalClassValue, finalTermDescription, savedResults; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docDir = [paths objectAtIndex:0]; 
    NSString *fullPath = [docDir stringByAppendingFormat:@"/%@", kFileName]; 
    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath]; 
    if (fileExists) { 
     savedResults = [NSKeyedUnarchiver unarchiveObjectWithFile:fullPath]; 
    } 
    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    if (savedResults) { 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES); 
     NSString *docDir = [paths objectAtIndex:0]; 
     NSString *fullPath = [docDir stringByAppendingFormat:@"/%@", kFileName]; 
     [NSKeyedArchiver archiveRootObject:savedResults toFile:fullPath]; 
    } 
} 

我把一個斷點在applicationDidEnterBackgroundMethod看看是怎麼回事。即使savedResults數組不爲空,我的程序也不會在if語句內輸入代碼塊。我也嘗試過測試如果([savedResults count]> 0)並且即使它大於零也不進入塊。這裏是XCode顯示的變量的圖片。正如你所看到的,數組中有對象。 bug bug2 我有一種感覺XCode正在查看上面的數組聲明,我將它設置爲nil而不是實際變量。我如何區分這兩個?任何幫助將不勝感激。謝謝!

回答

2

您有兩個變量名爲savedResults。一個是全局變量。另一個是PCFAppDelegate類中的實例變量,由@synthesize savedResults語句生成。調試器顯示你兩個變量。實例變量在self的擴展下,全局變量顯示在顯示三角形的右側,紅色框中顯示「S」。

所有提到的savedResults的方法PCFAppDelegate都使用了實例變量,但其他類中的提及將使用全局變量。因此,PCFAppDelegate之外的一些代碼將全局變量設置爲非零,但在 - [PCFAppDelegate applicationDidEnterBackground]中,您只能訪問仍然設置爲nil的實例變量。

+0

如何從AppDelegate類訪問全局變量?另外,我不需要複製變量,只需要一個。我是否應該註釋掉我的.h文件中的所有變量聲明,其中包括 部分,並保留@property? – kamran619

+0

最簡單的解決方法是完全擺脫'savedResults'屬性,只保留全局變量。 –

+0

工作,謝謝。我很疑惑什麼時候應該聲明一個屬性,以及什麼時候在我的.m文件中聲明它。 – kamran619