请参阅代码片段末尾的日志语句注释... acceleration.x是一个小数,但是当我将它存储到dict时,它似乎是0。
NSDictionary *entry = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithDouble:acceleration.x], @"x",
[NSNumber numberWithDouble:acceleration.y], @"y",
[NSNumber numberWithDouble:acceleration.z], @"z",
[NSDate date], @"date",
nil];
[self.accelerometerArray addObject: entry];
//this logs a decimal number as expected
NSLog(@"Acceleration.x: %g", acceleration.x);
//this logs a 0 :(
NSLog(@"Accel Array X: %g", [entry objectForKey: @"x"]);
答案 0 :(得分:3)
致电时:
[entry objectForKey: @"x"]
您返回NSNumber
实例,而不是double值。将该行更改为:
NSLog(@"Accel Array X: %g", [[entry objectForKey: @"x"] doubleValue]);
您将无法直接从NSDictionary
检索double,因为键和值只能是对象,而不是基本类型。