10

こんにちはスタック-エキスパート!

私の質問:CLLocationDegrees値から文字列を生成する方法は?

失敗した試行:

1. NSLog(@"Value: %f", currentLocation.coordinate.latitude); //Tried with all NSLog specifiers.
2. NSNumber *tmp = [[NSNumber alloc] initWithDouble:currentLocation.coordinate.latitude];
3. NSString *tmp = [[NSString alloc] initWithFormat:@"%@", currentLocation.coordinate.latitude];

CLLocationDegreesの定義を見ると、これはdoubleであることが明確に示されています。

typedef double CLLocationDegrees;

ここで何が欠けていますか?これは私を夢中にさせています...私の心を救うのを手伝ってください!

よろしくお願いします。// Abeansits

4

3 に答える 3

35

これらは正しいです:

NSLog(@"Value: %f", currentLocation.coordinate.latitude); //Tried with all NSLog specifiers.
NSNumber *tmp = [[NSNumber alloc] initWithDouble:currentLocation.coordinate.latitude];

これは間違っています。なぜなら、coordinate.latitudeは、nsstringが期待するようなオブジェクトではないからです。

NSString *tmp = [[NSString alloc] initWithFormat:@"%@", currentLocation.coordinate.latitude];

NSStringが必要な場合:

myString = [[NSNumber numberWithDouble:currentLocation.coordinate.latitude] stringValue];

また

NSString *tmp = [[NSString alloc] initWithFormat:@"%f", currentLocation.coordinate.latitude];

マルコ

于 2009-08-25T15:30:05.263 に答える
2

Swiftバージョン:

文字列への緯度:

var latitudeText = "\(currentLocation.coordinate.latitude)"

また

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)
于 2016-07-21T21:23:09.957 に答える
0

Obj-C形式

[[NSString alloc] initWithFormat:@"%f", coordinate.latitude];

Swiftフォーマット

String(format: "%f", coordinate.latitude)
于 2019-06-26T18:14:36.213 に答える