2012-04-30 12 views
0

私のエンティティが空である理由を教えてください。CoreData、私のエンティティは空です..なぜですか?

私は、私のJSONと私のエンティティの属性を、私のエンティティを移入していますが、私のJSONと同じ名前を持つ

これは私の実体を移入するために私のコードです:

NSManagedObjectContext *cxt = [self managedObjectContext]; 
    NSManagedObject *newBoxes = [NSEntityDescription insertNewObjectForEntityForName:@"Boxes" inManagedObjectContext:cxt]; 
    NSDictionary *parsedFeed = [[newBoxes entity] attributesByName]; 
    for (NSString *key in parsedFeed) { 
     id value = [parsedFeed objectForKey:key]; 
     // Don't assign NSNull, it will break assignments to NSString, etc. 
     if (value && [value isKindOfClass:[NSNull class]]) 
      value = nil; 

     @try { 
      [Boxes setValue:value forKey:key]; 
     } @catch (NSException *exception) { 
      // Exception means such attribute is not defined in the class or some other error. 
     } 
    } 

    NSError *err; 
    if (![cxt save:&err]) { 
     NSLog(@"An error has occured: %@", [err localizedDescription]); 
    } 



NSLog(@"ENTITY %@", newBoxes); 

これが結果です私のNSLog:

2012-04-30 11:16:23.352 Wonder[9987:fb03] ENTITY <Boxes: 0x6d87330> (entity: Boxes; id: 0x6d8b010 <x-coredata://EFDCC0BA-644D-42AC-8DE8-452F02B7C680/Boxes/p26> ; data: { 
    codeArticle = nil; 
    descriptionBox = nil; 
    dlu = 0; 
    kindBox = nil; 
    nameBox = nil; 
    nbActivities = 0; 
    note = 0; 
    priceBox = 0; 
    texteMarketing = nil; 
    typeBox = nil; 
}) 

これは私のJSONです:

{ 
    "totalBox":{ 
     "boxes":[ 
     { 
     "codeArticle": "WPCDE01C412L", 
     "nameBox": "boxName", 
     "texteMarketing": "boxTextMarketing", 
     "descriptionBox" : "boxDescritpion", 
     "nbActivities": 1650, 
     "kindBox": "boxKind", 
     "typeBox": "boxType", 
     "priceBox": 20, 
     "dlu": 2014, 
     "note": 3 
     }, 
     { 
     "codeArticle": "BOOYAKA!!", 
     "nameBox": "boxNameName", 
     "texteMarketing": "boxTextMarketing", 
     "descriptionBox" : "boxDescritpion", 
     "nbActivities": 1650, 
     "kindBox": "boxKind", 
     "typeBox": "boxType", 
     "priceBox": 39, 
     "dlu": 2014, 
     "note": 3 
     } 
     ] 
      } 
} 

これは私の実体である:

enter image description here

EDIT:私は私のJSONを与えるので、私はそれが正しい、私のエンティティを移入するために読むべきものを私coredataを「伝える」必要がありますか?

dataToDisplay = [[NSMutableArray alloc] init]; 

//récupération du chemin vers le fichier contenant le JSON 
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"JSON" ofType:@"txt"]; 

//création d'un string avec le contenu du JSON 
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]; 

//Parsage du JSON à l'aide du framework importé 
NSDictionary *json = [myJSON JSONValue]; 

//récupération du total des Boxes 
NSDictionary *resultats = [json objectForKey:@"totalBox"]; 

答えて

1

上記のコードでは、非オブジェクトに値を設定しようとしています。

[Boxes setValue:value forKey:key]; 

であるべき

[newBoxes setValue:value forKey:key]; 
私はあなたが http://rentzsch.github.com/mogenerator/を見て、JSONとCoreDataでこの超便利記事 http://www.cimgf.com/2012/01/11/handling-incoming-json-redux/

乾杯を取ることをお勧めします

mbogh

+0

ありがとうございました。私は間違いを犯していましたが、それでも私のエンティティを埋め尽くしていません。 N)しかし、最終的に私は "私はJSONを与えていないんだよ"と思っていました。 – user1256827

+0

JSONのコンテンツにはまったくアクセスしていないようですね、NSDictionary * parsedFeed = [[newBoxes entity ] attributesByName] ;.これはちょうどNSManagedObjectエンティティからすべてのキーを取得します... – mbogh

+0

私のCoredataを入力ですか?私のjsonと? 私は真剣に失われています、私は助けが必要です:/私はちょうど私のjsonで私のcoredataを設定したい、その後、私はいくつかの要求を行うでしょう:私は価格> 25€を持っているすべてのcodeArticleが必要です。 [ボックスsetValuesForKeysWithDictionary:json]; – user1256827

2

IエンティティにJSONを設定していると、エンティティの属性に私のJSONと同じ名前

質問に投稿したコードはありません。

このライン:

NSManagedObject *newBoxes = [NSEntityDescription insertNewObjectForEntityForName:@"Boxes" inManagedObjectContext:cxt]; 

は新しい管理対象オブジェクトを作成します。データはありません。

このコード:

NSDictionary *parsedFeed = [[newBoxes entity] attributesByName]; 

は、すべての管理対象オブジェクトの属性名と属性の説明のディクショナリを取得します。これはモデルの一部です。なぜあなたはそれをまったく気にかけていないのですか?

このコード

for (NSString *key in parsedFeed) { 
    id value = [parsedFeed objectForKey:key]; 
    // Don't assign NSNull, it will break assignments to NSString, etc. 
    if (value && [value isKindOfClass:[NSNull class]]) 
     value = nil; 

    @try { 
     [Boxes setValue:value forKey:key]; 
    } @catch (NSException *exception) { 
     // Exception means such attribute is not defined in the class or some other error. 
    } 
} 

は非常に奇妙です。あなたは属性記述(コアデータに属性、例えば文字列、数字などを伝える)にアクセスし、その値にBoxesのキーを設定します。私はBoxesがボックスをモデル化するために作成したNSManagedObjectのサブクラスだと推測しています。例外をキャッチすることに決めていない場合は、「セレクタに応答しません」というコードがここでクラッシュすることが予想されます。

基本的に、コード内で行うことは何の意味もありません。あなたがする必要があるのは、JSONのNSDictionaryを取得することです(NSJSONSerializationを参照)。現在、parsedFeedを使用している場所で使用してください。次に、あなたのコードは、多かれ少なかれ仕事(newBoxesBoxesを交換しますが、私は変更のカップルをお勧めします:。

NSDictionary *parsedFeed = [NSJSONSerialization JSONObjectWith....]; // can use an NSData or a stream 
for (NSString *key in parsedFeed) { 
    id value = [parsedFeed objectForKey:key]; 
    // Don't assign NSNull, it will break assignments to NSString, etc. 
    if ([value isEqual:[NSNull null]]) // nil is already handled OK 
     value = nil; 

    if ([legalKeys containsObject: key]) // see comment below 
    { 
     [newBoxes setValue: value forKey: key]; 
    } 
    else 
    { 
     // report bad data 
    }  
} 

legalKeysキーを合法的に設定することができたと言いますが、あなたのソースコードで作成したセットです例えば、NSObjectにはscriptingPropertiesという名前のプロパティがあります。おそらく、受信したJSONを

関連する問題