2011-02-03 59 views
0

我開始學習Objective-C並幫助我這樣做,我正在使用「Head First iPhone Development」。現在我正在學習SQLite數據庫,並讓它們起作用我聽說SQLite文件需要位於應用程序的Documents文件夾中,因此我必須移動該文件。將SQLite從一個地方移動到另一個地方:應用程序崩潰和不兼容類型

我正在使用書中的例子,但似乎無法使其工作。每次我編譯它,我的應用程序崩潰。我有以下警告:「不兼容的Objective-C類型初始化 '結構NSURL *',預期 '結構的NSString *'

有沒有人有一個提示如何解決這一問題

編輯:

問題似乎是在applicationDocumentsDirectory返回一個NSURL的這兩行中,但我告訴它返回一個NSString。我可以告訴它返回一個NSURL,但是在使用stringByAppendingPathComponent的下一行時會出現問題。方法來解決這個問題?

NSString *documentsDirectory = [self applicationDocumentsDirectory]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"iBountyHunter.sqlite"]; 

這就是調試器控制檯,當應用程序崩潰輸出:

2011-02-04 07:33:42.126 iBountyHunter[591:207] -[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x6043f80 
2011-02-04 07:33:42.128 iBountyHunter[591:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL stringByAppendingPathComponent:]: unrecognized selector sent to instance 0x6043f80' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x00f87be9 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x010dc5c2 objc_exception_throw + 47 
    2 CoreFoundation      0x00f896fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x00ef9366 ___forwarding___ + 966 
    4 CoreFoundation      0x00ef8f22 _CF_forwarding_prep_0 + 50 
    5 iBountyHunter      0x00001d8e -[iBountyHunterAppDelegate createEditableCopyOfDatabaseIfNeeded] + 107 
    6 iBountyHunter      0x00001f24 -[iBountyHunterAppDelegate application:didFinishLaunchingWithOptions:] + 37 
    7 UIKit        0x002ba1fa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163 
    8 UIKit        0x002bc55e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439 
    9 UIKit        0x002c6db2 -[UIApplication handleEvent:withNewEvent:] + 1533 
    10 UIKit        0x002bf202 -[UIApplication sendEvent:] + 71 
    11 UIKit        0x002c4732 _UIApplicationHandleEvent + 7576 
    12 GraphicsServices     0x018bda36 PurpleEventCallback + 1550 
    13 CoreFoundation      0x00f69064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
    14 CoreFoundation      0x00ec96f7 __CFRunLoopDoSource1 + 215 
    15 CoreFoundation      0x00ec6983 __CFRunLoopRun + 979 
    16 CoreFoundation      0x00ec6240 CFRunLoopRunSpecific + 208 
    17 CoreFoundation      0x00ec6161 CFRunLoopRunInMode + 97 
    18 UIKit        0x002bbfa8 -[UIApplication _run] + 636 
    19 UIKit        0x002c842e UIApplicationMain + 1160 
    20 iBountyHunter      0x00001cf8 main + 102 
    21 iBountyHunter      0x00001c89 start + 53 
) 
terminate called after throwing an instance of 'NSException' 

回答

0

這將做到這一點。

// Check the existence of database 
     NSFileManager *mngr = [[NSFileManager alloc] init]; 

     // If the database doesn't exist in our Document folder we copy it to Documents (this will be executed only the first time we launch the app). 
     if (![mngr fileExistsAtPath:[NSString stringWithFormat:@"%@/Documents/db.sqlite", NSHomeDirectory()]]){ 
      NSString *filePath = [[NSBundle mainBundle] pathForResource:@"db.sqlite" ofType:nil]; 
      [mngr copyItemAtPath:filePath toPath:[NSString stringWithFormat:@"%@/Documents/db.sqlite", NSHomeDirectory()] error:NULL]; 
     } 
     [mngr release]; 

這裏的applicationDocumentsDirectory方法的另一個版本:

+ (NSString*)applicationDocumentsDirectory { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    return documentsDirectory; 
} 
1

我使用從書 的例子,但我似乎無法得到它的工作。 每次我編譯它我的應用程序崩潰。 我有以下警告: 「不兼容的Objective-C類型 初始化‘結構NSURL *’, 預期‘結構的NSString *’

修復警告;你的東西,期望一個分配一個NSURL實例NSString的實例。

或發佈警告&線,導致它的代碼。

如果您的應用程序崩潰,後墜毀。


的問題似乎是在這兩個 線, applicationDocumentsDirectory 返回一個NSURL,但我告訴它 返回一個NSString。我可以告訴它 返回一個NSURL,但是這給我一個 問題,我在下一行使用 stringByAppendingPathComponent。有 有辦法解決這個問題嗎?

你不能告訴一個方法返回一個NSURL來返回一個NSString。無論是鑄造型的返回值,也不象下面的分配將工作:

NSString *documentsDirectory = [self applicationDocumentsDirectory]; 

編譯器在該行警告,因爲它是壞了;你不能像NSString那樣對待NSURL。

像下面的東西應該工作:

NSString *documentsDirectory = [[self applicationDocumentsDirectory] path]; 
+0

當然是的。 NSURL和NSString不可互換。編輯您的問題以顯示編譯錯誤的位置。 – bbum 2011-02-04 06:30:34

+0

我已經更新了問題以顯示調試器控制檯中的輸出。 – simonbs 2011-02-04 06:36:59

0

我有以下警告:「不兼容的Objective-C類型初始化 '結構NSURL *',預期 '結構的NSString *'

問題是,你正在調用蘋果提供的applicationDocumentsDirectory方法,它返回一個NSURL。

+0

是否有可能使它返回一個NSString或使用帶NSURL的stringByAppendingPathComponent? – simonbs 2011-02-04 06:31:50

1

如果應用程序仍然在你的模擬器doen't工作,千萬記得要刪除的應用程序從你的模擬器,然後生成並運行應用程序再次。

您可以像使用真實設備一樣從模擬器中刪除應用程序。

相關問題