Dans le code suivant, la première instruction de journal affiche une décimale comme prévu, mais la seconde enregistre NULL. Qu'est-ce que je fais mal?Création d'un dictionnaire NSDictionary
NSDictionary *entry = [[NSDictionary alloc] initWithObjectsAndKeys:
@"x", [NSNumber numberWithDouble:acceleration.x],
@"y", [NSNumber numberWithDouble:acceleration.y],
@"z", [NSNumber numberWithDouble:acceleration.z],
@"date", [NSDate date],
nil];
NSLog([NSString stringWithFormat:@"%@", [NSNumber numberWithDouble:acceleration.x]]);
NSLog([NSString stringWithFormat:@"%@", [entry objectForKey:@"x"]]);
Sur une note sans rapport, les [NSString stringWithFormat:] bit est inutile et potentiellement dangereux. Vous devriez appeler NSLog comme ceci: NSLog (@ "% @", [entry objectForKey: @ "x"]) ;. Le premier paramètre de NSLog est une chaîne de format, qui devrait presque toujours être un littéral. –