当全局变量不是时,它被识别为null - 目标C.

时间:2012-11-04 20:57:55

标签: ios cocoa-touch static nsmutablearray scope

我在Objective-C程序中遇到了一个令人困惑的问题。我正在尝试在程序进入后台时从NSMutableArray中保存数据。我的AppDelegate中有一个名为savedResults的静态变量。视图控制器操作此变量并在程序的生命周期内向其添加数据。我有一个逻辑条件来检查savedResults是否为null,如果不是,那么我需要保存数据。这是我的代码:

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中放了一个断点来查看发生了什么。我的程序永远不会进入if语句中的代码块,即使savedResults数组是非null。我也试过测试if([savedResults count]> 0)并且它没有进入块,即使它大于零。这是XCode显示的变量的图片。如您所见,数组中有ARE对象。 bug bug2  我有一种感觉XCode正在查看上面的数组声明,我将其设置为nil而不是实际变量。我如何区分这两个?任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:2)

您有两个名为savedResults的变量。一个是全局变量。另一个是PCFAppDelegate类中的实例变量,由@synthesize savedResults语句生成。调试器显示两个变量。实例变量在self的扩展下,全局变量显示在显示三角形的右侧,而“S”显示在红色框中。

savedResults方法中PCFAppDelegate的所有提及都使用实例变量,但其他类中的提及将使用全局变量。因此PCFAppDelegate之外的一些代码将全局变量设置为非零,但在 - [PCFAppDelegate applicationDidEnterBackground]`中,您只能访问实例变量,该变量仍设置为nil。 / p>