我是一名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的定義嗎? –