【发布时间】:2011-11-15 15:00:04
【问题描述】:
我是一名 Objective-C 新手,正在学习 iPhone 编程。
在我的 appDelegate 中,在 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中,我有一个名为 databasePath 的类成员 (@syntethized)。
我这样设置它的值:
databasePath = [self copyDatabaseToDocuments];
我从 Alasdair Allan 的一本好书中复制了整个 copyDatabaseToDocuments 方法,并且只做了很少的更改(数据库的名称是我唯一更改的):
-(NSString *)copyDatabaseToDocuments{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath=[paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myDb.sqlite"];
//
if(![fileManager fileExistsAtPath:filePath]){
NSString *bundlePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myDb.sqlite"];
[fileManager copyItemAtPath:bundlePath toPath:filePath error:nil];
}
return filePath;
}
我 NSLog databasePath 并在分配后定期获取它的值(它是一个字符串路径,它不为空)。
然后,我有一个方法 -(NSMutableArray*)readDatabase:(char*)querySQL 我通过委托引用从 ViewController 调用。
如果 - 在最后一个方法内 - 我再次分配 databasePath 的值,任何事情都可以正常工作。
但是,如果我不再次分配它并且我想使用它的值(我想它是在 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中设置的),应用程序就会崩溃。
为什么?
【问题讨论】:
-
能把@property的定义写在databasePath的头文件里吗?
标签: iphone objective-c