iPhone:变量范围问题

发布于 2024-10-18 03:26:41 字数 1590 浏览 1 评论 0原文

我使用此代码从 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];
}

当没有可从中获取数据的 plist 文件时,第一次一切加载正常。但在第二次加载时,当我尝试在 loadOrCreate 方法之外使用加载的数据时,应用程序崩溃了。由于某种原因,在 loadOrCreate 方法外部访问时,字体、背景和动画中的数据不可用。变量 - 字体、背景和动画在 .h 文件中声明为 NSString,因此应该全局可用,对吗?您能告诉我这是什么原因吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2024-10-25 03:26:41

您必须保留该对象。

    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];

注意:如果您多次加载数据,请不要忘记在设置值之前释放它们。

编辑:

-(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");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文