Je voudrais imprimer une entité. J'ai un JSON, j'obtiens la valeur, placez-les dans mon entité. Maintenant, je veux afficher mon entité pour voir si elle est bien définie, comment?Comment imprimer une entité - CoreData
voici mon code, il semble fonctionner, le NSLOG imprimer ce que je veux. cool.
- (void)viewDidLoad
{
[super viewDidLoad];
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"];
//récupération du tableau de Boxes
NSArray *listBoxes = [resultats objectForKey:@"boxes"];
//On parcourt la liste de boxes
for (NSDictionary *dic in listBoxes) {
getBoxes *boxes = [[getBoxes alloc] init];
boxes.nameBox = [dic objectForKey:@"boxName"];
//renseingement du score
boxes.priceBox = [dic objectForKey:@"boxPrice"];
boxes.kindBox = [dic objectForKey:@"boxKind"];
boxes.typeBox = [dic objectForKey:@"boxType"];
//ajout à la liste
[dataToDisplay addObject:boxes];
//libération de la mémoire
[boxes release];
}
NSLog(@"boxKind %@", [[listBoxes objectAtIndex:1] valueForKey:@"boxKind"]);
//à ne pas oublier après l'allocation effectuée au début
// TEST
NSManagedObjectContext *cxt = [self managedObjectContext];
NSManagedObject *newBoxes = [NSEntityDescription insertNewObjectForEntityForName:@"Boxes" inManagedObjectContext:cxt];
[newBoxes setValue:[[listBoxes objectAtIndex:1] valueForKey:@"boxKind"] forKey:@"kindBox"];
[newBoxes setValue:[[listBoxes objectAtIndex:1] valueForKey:@"boxPrice"] forKey:@"priceBox"];
[newBoxes setValue:[[listBoxes objectAtIndex:1] valueForKey:@"boxType"] forKey:@"typeBox"];
[newBoxes setValue:[[listBoxes objectAtIndex:1] valueForKey:@"boxName"] forKey:@"nameBox"];
NSError *err;
if (![cxt save:&err]) {
NSLog(@"An error has occured: %@", [err localizedDescription]);
}
[myJSON release];
NSLog(@"%@", [newBoxes valueForKey:@"kindBox"]);
NSLog(@"%@", [newBoxes valueForKey:@"priceBox"]);
NSLog(@"%@", [newBoxes valueForKey:@"typeBox"]);
NSLog(@"%@", [newBoxes valueForKey:@"nameBox"]);
}
@Flink: Pourquoi u a commenté comme ça? Je veux dire que NSManagedObject est là pour dire qu'il utilise des coredata. –