0

次のような変換可能なコアデータオブジェクトのプロパティにアクセスしようとしています。

MainObject *localObject = [self.myArray objectAtIndex:indexPath.row];
TransformableObject *actualObject = localObject.transformableObject;

これは正常に機能します。私NSLog actualObjectと正しいキー/値のペアを取得します:

actualObject:{

    property = "Foo";
    property2 = "Apple";
}

ただし、IIが次のことを試みる場合:

NSLog (@"property:%@",actualObject.property)私は得る-[__NSCFDictionary property]: unrecognized selector sent to instance

私は何が間違っているのですか?私のコアデータクラスには、次のものがあります。

@interface MainObject : NSManagedObject

@property (nonatomic, retain) TransformableObjectClass *transformableObject;

編集:これが私がCoreDataでオブジェクトを設定する方法です

        MainObject *event = (MainObject *)[NSEntityDescription insertNewObjectForEntityForName:@"MainObject" inManagedObjectContext:[AppDelegateMy managedObjectContext]];
        [event setValuesForKeysWithDictionary:[myArrray objectAtIndex:i]];
4

1 に答える 1

1

-setValuesForKeysWithDictionary:Core Data も Core Data も、あなたの側でさらに努力しなくてもこの作業を行うのに十分な魔法ではありません。現時点では、JSON 応答のディクショナリを新しい管理対象オブジェクトのtransformableObjectプロパティに割り当てているだけです。コンテキストを保存しようとすると、おそらく失敗すると思います。これらの辞書を実際のインスタンスに変換したい場合TransformableObjectClassは、自分で行う必要があります。

アプリにこの種のものがたくさんある場合は、RestKit 、またはClassMapperのような他のオープンソース JSON-to-object マッピング ライブラリの 1 つを調べることをお勧めします。

于 2012-09-06T02:45:51.167 に答える