2009-08-25 5 views
10

こんにちはスタックエキスパート!CLLocationDegreesから文字列を生成します。 NSLogまたはStringWithFormatで

私の質問: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の定義に見ると、それは明らかに、これは二重であると述べている:

typedef double CLLocationDegrees; 

私はここで何をしないのですか?これは私が夢中になっている...私の心を救うために助けてください!

ご協力いただきありがとうございます。 // Abeansits

答えて

33

これらは正しいです:coordinate.latitudeが期待するかもしれないNSStringのようなオブジェクトではありませんので、

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

これは、間違っています。

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]; 

マルコ

+0

はあなたのマルコをありがとう!信用できる。 私のエラーは実際にはメモリの割り当てに問題があります。 =( 申し訳ありません。 – ABeanSits

+3

@Marcoあなたはスワイプコードplzでこれを翻訳できますか? –

2

スウィフトバージョン:

ラ文字列へのtitude:

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

または

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude) 
関連する問題