-1

データコアを使用する xcodeproj を再利用しようとしています。それを新しいプロジェクトにインポートし、いくつかのクラスの初期化を行おうとすると、次のエラーが発生しました

'NSInvalidArgumentException', reason: ' * -[NSURL initFileURLWithPath:]: nil string parameter' エラーが、de managedObjectModel の設定時に発生しました。

私のコードは次のとおりです。

- (NSManagedObjectModel *)managedObjectModel {
    if (managedObjectModel == nil) {

        NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"momd"];
        NSURL    *modelURL  = nil;

        if (modelPath == nil)
            modelPath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"mom"];
        modelURL           = [NSURL fileURLWithPath:modelPath];
        managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    }

    return managedObjectModel;
}

エラーの原因となっている行は

modelURL           = [NSURL fileURLWithPath:modelPath];

DataModel が見つからないようです。私のデータモデルファイルはDataModel.xcdatamodelと呼ばれています

どうしたの?私は何か重要なものを見逃していますか?

どうもありがとう。

4

3 に答える 3

0

ファイルタイプは。momである必要がありますmomd

次に、以下のようにmodelPathまたはmodelURLを出力して、nilを確認します。nilをに渡す modelURL = [NSURL fileURLWithPath:modelPath];

 NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"momd"];
        NSLog(@"%@",modelPath);

    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DataModel" withExtension:@"momd"];
    NSLog(@"%@",modelURL);
于 2012-09-10T09:23:17.623 に答える
0

さて、私はそれを解決しました。

AppDelegate と同じグループに DataModel.xcdatamodel のコピーが必要でした。だから私は他のxcodeprojからそれを追加し、最終的にそれは働いた

于 2012-09-10T10:33:53.477 に答える
0

あなたは に渡っnilfileURLWithPathいます。

あなたのコードから、これはそれを意味します

   modelPath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"mom"];

は に割り当てnilられるmodelPathため、データ モデル ファイルの正しい名前を確認できます。

データ モデル ファイルがターゲットに含まれていることも確認してください。

于 2012-09-10T09:19:35.050 に答える