4

xcode 3.2.5 で新しい coredata プロジェクトを開始した後、いくつかの問題が発生しました... (以前の xcode で) コア データを使用した以前のプロジェクトは正常に機能したため、何が違いなのかわかりません??

したがって、ビルドしてコアデータを呼び出すビューに移動すると、エラーが発生します>

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '***  -[NSURL initFileURLWithPath:]: nil string parameter'

奇妙なことは、私の *AppDelegate.m にあることです (Rog に感謝しますが、まだ機能していません!)

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (persistentStoreCoordinator_ != nil) {
    return persistentStoreCoordinator_;
}   

NSString *storePath = [[self applicationDocumentsDirectory]   stringByAppendingPathComponent: @"staff.sqlite"];

NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; //new position for the storeUrl!    

// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"staff" ofType:@"sqlite"];
    if (defaultStorePath) {
        [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
    }
}

の中に

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"];

警告が表示されます

NSURL may not respond to '-stringByAppendingPathComponent'

このstringByAppendingPathComponentをオプション+クリックして取得します(シンボルが見つかりません!!!!)

しかし、他のプロジェクトでは、同じようにオプション+クリックして定義を取得します!!

  • この警告は私のエラーに関連していますか??
  • それを修正する方法は?

編集、これを私のviewDidLoadに含めました

NSLog(@"path= %@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]) ;

これにより、コンソールに次のように表示されます。

path= /Users/mkss9/Library/Application Support/iPhone Simulator/4.2/Applications/2F364C20-2B87-4ABB-AA3E-FB6F7C15096F/Documents

ください!, 私は狂っている !!

ありがとう!

4

3 に答える 3

7

一部のSDKバージョンより前(いつだったかはわかりません)、AppleはapplicationDocumentsDirectoryプロジェクトテンプレートの戻り値の型を変更しました。

新しいプロジェクトを作成すると、次のようになります。

/**
 Returns the URL to the application's Documents directory.
 */
- (NSURL *)applicationDocumentsDirectory {
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

古いテンプレートでは、次のようになりました。

/**
 Returns the path to the application's documents directory.
 */
- (NSString *)applicationDocumentsDirectory {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

そして、これら2つの間には次のように見えました:

/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

NSString を返す applicationDocumentsDirectory に依存するすべての古いコードは、新しいテンプレートでは機能しないため、注意が必要です。

また、コア データ メソッドが変更されるため、新しいバージョンを古いバージョンに置き換えることはできません。

そのため、ドキュメント ディレクトリを返すための独自のメソッドを作成することをお勧めします。AppleはapplicationDocumentsDirectoryかなり頻繁に変更します。

于 2011-02-11T08:00:59.967 に答える
1

の代わりに を-applicationDocumentsDirectory返すためだと思います。NSURL *NSString *

于 2011-02-11T02:03:44.813 に答える
0

applicationDocumentsDirectoryまず、メソッドがNSStringを返していることを確認する必要があります。

それが邪魔にならない場合、その後のクラッシュは、まだ存在していないパスとファイル名を渡しているためです。

したがってNSURL *storeUrl = [NSURL fileURLWithPath:storePath];、既存のファイルをチェックし、存在しない場合にデフォルトのファイルを配置するコードの後に​​移動すると、問題が解決するはずです。

于 2011-02-11T02:38:37.843 に答える