我是一名Objective-C新手,正在学习iPhone编程。尝试访问appDelegate会员时,iPhone应用程序崩溃
在我的appDelegate,在-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法,我有一个类成员(@syntethized)称为databasePath。
我将其值设置是这样的:
databasePath = [self copyDatabaseToDocuments];
我从一个极好的书阿拉斯代尔·阿伦复制整个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
方法中设置的),那么应用程序会崩溃。
为什么?
您可以在databasePath的头文件中编写@property的定义吗? –