Генерирование строки из CLLocationDegrees, например. в NSLog или StringWithFormat
Hello Stacked-Experts!
Мой вопрос: как сгенерировать строку из значения 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
Ответы
Ответ 1
Это верно:
NSLog(@"Value: %f", currentLocation.coordinate.latitude); //Tried with all NSLog specifiers.
NSNumber *tmp = [[NSNumber alloc] initWithDouble:currentLocation.coordinate.latitude];
Это неверно, потому что координата .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
Ответ 2
Быстрая версия:
Широта для строки:
var latitudeText = "\(currentLocation.coordinate.latitude)"
или
let latitudeText = String(format: "%f", currentLocation.coordinate.latitude)
Ответ 3
Формат Obj-C
[[NSString alloc] initWithFormat:@"%f", coordinate.latitude];
Свифт формат
String(format: "%f", coordinate.latitude)