我想打印一个实体。
我有一个 JSON,我得到了值,将它们设置在我的实体中。现在我想显示我的实体以查看它是否设置正确,如何?
这是我的代码,它似乎工作,NSLOG 打印我想要的。凉爽的。
- (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"]);
}