在下面的代碼中,第一個日誌語句顯示爲預期的小數,但第二個日誌爲NULL。我究竟做錯了什麼?創建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"]]);
在一個不相關的說明中,[的NSString stringWithFormat:]位是不必要的,並且可能有害。你應該像這樣調用NSLog:NSLog(@「%@」,[entry objectForKey:@「x」]);. NSLog的第一個參數是一個格式字符串,它應該總是一個文字。 – 2009-07-10 07:36:21