在下面的代码中,第一个日志语句按预期显示小数,但第二个日志为NULL。我究竟做错了什么?
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"]]);
最佳答案
您正在交换插入对象和键的顺序:您需要先插入对象,然后再插入键,如以下示例所示。
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];
关于iphone - 创建一个NSDictionary,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1107996/