你能帮我告诉我为什么我的实体是空的吗?
我正在用我的 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
}
]
}
}
这是我的实体:
编辑:我给了我的 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"];