2009-08-25 8 views
10

Witaj ułożone ekspertów!Generowanie ciągu z CLLocationDegrees, np. w NSLog lub StringWithFormat

Moje pytanie: Jak wygenerować ciąg znaków z wartości CLLocationDegrees?

Nieudane próby:

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

Kiedy patrzę w definicji dla CLLocationDegrees jasno stwierdza, że ​​jest to podwójnie:

typedef double CLLocationDegrees; 

Co ja tu brakuje? To doprowadza mnie do szaleństwa ... Pomóż mi uratować mój umysł!

Z góry dziękuję i pozdrawiam. // Abeansits

Odpowiedz

33

Są poprawne:

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

To jest złe, bo coordinate.latitude nie jest obiektem jako NSString może spodziewać.

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

Jeśli chcesz NSString:

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

lub

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

Marco

+0

Dziękuję Marco! To prawda. Mój błąd polegał na nieprawidłowym przydzieleniu pamięci. = ( Przepraszam, – ABeanSits

+3

@Marco możesz przetłumaczyć to szybkim kodem plz –

2

Swift wersja:

La titude String:

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

lub

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