开发者

iPhone: Problem with scope of variables

开发者 https://www.devze.com 2023-02-12 00:29 出处:网络
I have used this code to load data from a plist. -(void)loadOrCreateData { NSLog(@\"Start loadOrCreateData\");

I have used this code to load data from a plist.

-(void)loadOrCreateData {
    NSLog(@"Start loadOrCreateData");
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    {
        NSLog(@"File Exists.. Loading from plist File");
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        font = [array objectAtIndex:0];
        background = (NSString *)[array objectAtIndex:1];
        animation = [array objectAtIndex:5];
        [array release];
        NSLog(@"Loading Done!");
    }
    else 
    {
        NSLog(@"File does not exist.. Creating new plist File");
        font = @"Georgia-BoldItalic";
        background = @"monalisa.jpeg";
        animation = @"103";
        [self saveData];
    }

    NSLog(@"Finish loadOrCreateData");
}

- (NSString *)dataFilePath {
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   开发者_运维百科return [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
}

- (void)saveData {
   NSMutableArray *array = [[NSMutableArray alloc] init];
   [array addObject:font];
   [array addObject:background];
   [array addObject:animation];
   [array writeToFile:[self dataFilePath] atomically:YES];
   [array release];
}

The first time everything loads fine when there is no plist file to get data from. But on second load, the app crashes when I try to use the loaded data outside the loadOrCreate method. For some reason, the data in font, background and animation is not available when accessed outside loadOrCreate method. The variables - font, background and animation are declared as NSStrings in the .h file and therefore should be globally available right? Can you please tell me what the reason for this is?


You have to retain that objects.

    font = [[array objectAtIndex:0] retain];
    background = (NSString *)[[array objectAtIndex:1] retain];
    animation = [[array objectAtIndex:5] retain];
...
    font = [@"Georgia-BoldItalic" retain];
    background = [@"monalisa.jpeg" retain];
    animation = [@"103" retain];

Note: if you're loading data several times, then don't forget to release values before you set them.

Edit:

-(void)loadOrCreateData {
    [font release];
    [background release];
    [animation release];

    NSLog(@"Start loadOrCreateData");
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    {
        NSLog(@"File Exists.. Loading from plist File");
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        font = [[array objectAtIndex:0] retain];
        background = (NSString *)[[array objectAtIndex:1] retain];
        animation = [[array objectAtIndex:5] retain];
        [array release];
        NSLog(@"Loading Done!");
    }
    else 
    {
        NSLog(@"File does not exist.. Creating new plist File");
        font = [@"Georgia-BoldItalic" retain];
        background = [@"monalisa.jpeg" retain];
        animation = [@"103" retain];
        [self saveData];
    }

    NSLog(@"Finish loadOrCreateData");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消