2011-02-03 6 views
0

Objective-Cを学び始めたので、私はこれを手助けするために「Head First iPhone Development」を使用しています。今私はSQLiteデータベースについて学習しており、これらを動作させるためには、SQLiteファイルをアプリケーションのDocumentsフォルダに置く必要があるため、ファイルを移動する必要があると言われています。SQLiteをある場所から別の場所に移動する:アプリケーションがクラッシュし、互換性のないタイプ

私は本の例を使用していますが、動作させることができません。コンパイルするたびに、アプリがクラッシュします。初期化「互換性のないObjective-Cのタイプ '構造体NSURLを*'、期待

'NSStringの*ストラクト' を誰もがこの問題を解決する方法の先端を持っています

EDIT:私は、次の警告がありますか?

NSURLを返すapplicationDocumentsDirectoryの2つの行に問題があるようですが、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

これはトリックを行います。アプリケーションがまだアプリケーションを構築し、実行し、シミュレータあなたからアプリケーションを削除することを忘れないでください、あなたのシミュレータで仕事をdoen't場合

+ (NSString*)applicationDocumentsDirectory { 

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

私はあなたの最初のコードを試しましたが、動作させることはできません。ファイルがすでに存在しているため、移動されていないと思っていますが、TableViewに表示されるデータはありません。他のバージョンのapplicationDocumentsDirectoryメソッドを実装するにはどうすればよいですか? – simonbs

+0

コードは私のアプリで動作します。 nslogを実行して、実際にdbにあるものを確認します。他のバージョンを使用するには、applicationDocumentsDirectory宣言を見つけてメソッドシグネチャを '+(NSSString *)'に変更し、コードを実装ファイルにコピーします。それでも自分自身で呼びたい場合は、+をaに変更する必要があります。クラスメソッドではなくインスタンスメソッドですが、 '[iBountyHunterAppDelegate applicationDocumentsDirectory] ​​'に変更しない理由はありません。 – Rich

+0

私は推測します目的の基礎に慣れていないので、テーブルビューに何も表示させないメモリ管理上の問題があると思われます。テーブルビューの優れたリソースは次のとおりです:http://code.google.com/p/iphone4/downloads/listこれは「Advanced iOS 4 Programming」と呼ばれる本当に良い本のソースコードです。 「Head First iPhone Development」に問題がある場合は、別の本を購入することを検討することがあります。テーブルビューのもう1つの優れた点は、「もっとiPhone 3の開発:iPhone SDKに取り組む」(やや高密度ですが)です。 – Rich

1

私は本 からの例を使用していますが、私はそれを動作させるように見えることはできません。 コンパイルするたびにアプリがクラッシュします。 私は次の警告を持っている: 「互換性のないObjective-Cのタイプ 『は構造体NSURL *』を初期化し、 期待 『ストラクトのNSString *』

警告を修正し、あなたが期待する何かにNSURLのインスタンスを割り当てますNSStringのインスタンス。

か、それを引き起こしたコードの警告&ラインを投稿してください。

あなたのアプリがクラッシュした場合、クラッシュを投稿してください。


問題がapplicationDocumentsDirectory はNSURLを返すと、これら2つの ライン内にあるように見えますが、私は リターンNSStringのにそれを伝えます。私は NSURLを返すことができますが、それは stringByAppendingPathComponentを使用する次の行に私に 問題を与える。 これを修正する方法はありますか?

代わりにNSStringのを返すようにNSURLを返す方法を教えてくれませんすることができます。戻り値をキャストする型も、次のような代入も機能しません。壊れているため、コンパイラはその行で警告します。 NSStringのようにNSURLを扱うことはできません。

次のようなものは動作するはずです:

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

もちろんです。 NSURLとNSStringは互換性がありません。あなたの質問を編集して、コンパイルエラーが解決された場所を表示してください。 – bbum

+0

デバッガコンソールで出力を表示するために質問を更新しました。 – simonbs

0

I次の警告があります:「初期化互換性のObjective-Cの型 '構造体NSURLを*'、期待が

'NSStringの*ストラクト' を

問題は、リンゴ提供のapplicationDocumentsDirectoryメソッドを呼び出すことで、NSURLを返します。

+0

NSStringを返すか、stringByAppendingPathComponentをNSURLで返すことはできますか? – simonbs

1

// 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方法の別のバージョンです再び。

実際のデバイスと同じ方法でアプリケーションをシミュレータから削除します。

関連する問題