Trong đoạn mã sau, câu lệnh nhật ký đầu tiên hiển thị số thập phân như mong đợi, nhưng các bản ghi thứ hai NULL. Tôi đang làm gì sai?Tạo một 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"]]);
Trên một lưu ý không liên quan, các [NSString stringWithFormat:] bit là không cần thiết, và có khả năng gây hại. Bạn nên gọi NSLog như thế này: NSLog (@ "% @", [entry objectForKey: @ "x"]) ;. Tham số đầu tiên cho NSLog là một chuỗi định dạng, mà chỉ cần luôn luôn là một chữ. –