2009-08-25 4 views
10

Hello Stacked-Experts!Создание строки из 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

Это правильно:

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

Marco

+0

Спасибо Марко! Это так. Моя ошибка на самом деле была связана с неправильным распределением памяти. = ( Извините. – ABeanSits

+3

@Marco вы можете перевести это в быстрый код plz –

2

Swift версия:

La titude в строку:

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

или

let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)