iPhone:变量范围问题
我使用此代码从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须保留该对象。
注意:如果您多次加载数据,请不要忘记在设置值之前释放它们。
编辑:
You have to retain that objects.
Note: if you're loading data several times, then don't forget to release values before you set them.
Edit: