2012-01-16 1 views
1

私はiOSアプリケーションの開発にかなり新しいです。エラーはソースパスがnilであると言います

私はDBにレコードを保存し、そこから取り出したサンプルDBアプリを構築しようとしています。

私はシミュレーターでテストする際にアプリケーションがうまく動作します。私はローカルデータベースを作成し、必要に応じてプログラムのコピーを作成しています。また、ローカルDBが 'バンドルリソースのコピー'で参照されていることを確認しました。

しかし、私は取得していますエラーは "*がキャッチされない例外により 'NSInvalidArgumentException'、理由にアプリを終了: '* - [NSFileManager copyItemAtPath:toPath:エラー:]:ソースパスがnilである'"、です。

それはシミュレータのための完璧な仕事が、私は私のデバイスでテストしない場合は、「[ゼロ:databasePathFromApp toPath:DBPATHエラーFileManagerのcopyItemAtPath]」

私はこの問題の原因となるコードの一部が だと思います。

助けを期待。私のコードである

- (id)init { 

    self = [super init]; 

    //Datbase Name 
    [email protected]"Person.sqlite3"; 

    //Getting DB Path 
    NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDir = [documentsPath objectAtIndex:0]; 

    DBPath=[documentsDir stringByAppendingPathComponent:DBName ]; 
    //DBPath = [[NSString alloc] initWithString: [documentsDir stringByAppendingPathComponent:DBName]]; 
    NSLog(@"DBPath is %@",DBPath); 

    return self; 

} 


-(void)checkAndCreateDB{ 

    BOOL Success; 

    //NSFileManager maintains File 
    NSFileManager *FileManager = [NSFileManager defaultManager]; 
    NSLog(@"line 1"); 

    //Checks Database Path 
    Success = [FileManager fileExistsAtPath:DBPath]; 
    NSLog(@"line 2"); 

    //If file exists, it returns true 
    if(Success)return; 
    NSLog(@"line 3"); 

    //NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName]; 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"]; 
    NSLog(@"line 4"); 

    [FileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; 
    //[FileManager release]; 
    NSLog(@"line 5"); 

} 

自分のアプリケーションがクラッシュしながらラインのNSLog(4行目)後のデバイスでテスト。しかし、シミュレータでうまくいく。 プラカシュ

答えて

0

デバイスはアプリケーションバンドルに書き込むことができない

多くのおかげで、あなたは、Documentsディレクトリを使用する必要があります。あなたが最初のDBを持っている場合は、アプリケーションバンドルは、初期起動時にドキュメントにコピーして

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 

:ここ

は、ドキュメントディレクトリのパスを取得する方法です。

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person" ofType:@"sqlite3"]; 

+0

を離れて取得しますが、iOSデバイスでは、大文字と小文字が区別されますクイック返信ありがとう:)私の既存のコードはまったく同じであり、私のローカルデータベースもドキュメントにあります。 NSArray *ドキュメントパス= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory、NSUserDomainMask、YES); NSString * documentsDir = [ドキュメントパスobjectAtIndex:0]; DBPath = [documentsDir stringByAppendingPathComponent:DBName]; まだデバイスでは機能しません。 – suj

0

変更この行を

NSString *databasePathFromApp=[[NSBundle mainBundle] pathForResource:@"Person.sqlite3" ofType:@"sqlite3"]; 

また、大文字と小文字に注意を払うシミュレータ上で、あなたは間違った総額で

+0

うわー!それは働いています:) 多くのありがとう、Xスラッシュ...ウルのアドバイスが働いた:) – suj

+0

それが動作することを聞いて良い、あなたは答えを受け入れることができますか?非常に感謝されます:) –

関連する問題