将CLLocationCoordinate2D类型转换为数字或字符串

ban*_*ing 5 cocoa cllocation

我想知道如何将CLLocationCoordinate2D的纬度和经度值转换为数字或字符串值.我尝试了几种不同的方法,但他们没有工作:

CLLocationCoordinate2D centerCoord;
centerCoord.latitude = self.locModel.userLocation.coordinate.latitude ;
centerCoord.longitude = self.locModel.userLocation.coordinate.longitude; 
NSString *tmpLat = [[NSString alloc] initWithFormat:@"%g", centerCoord.latitude];
NSString *tmpLong = [[NSString alloc] initWithFormat:@"%g", centerCoord.longitude];

NSLog("User's latitude is: %@", tmpLat);
NSLog("User's longitude is: %@", tmpLong);
Run Code Online (Sandbox Code Playgroud)

这会返回编译器的警告.

警告是

warning: passing argument 1 of 'NSLog' from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

任何帮助,将不胜感激.

谢谢

小智 7

你没有提到警告是什么,但最有可能是因为你忘记@了NSLog字符串的前面:

NSLog(@"User's latitude is: %f", self.locModel.userLocation.coordinate.latitude );
NSLog(@"User's longitude is: %f", self.locModel.userLocation.coordinate.longitude );
Run Code Online (Sandbox Code Playgroud)

您的更新代码应为:

NSLog(@"User's latitude is: %@", tmpLat);
NSLog(@"User's longitude is: %@", tmpLong);
Run Code Online (Sandbox Code Playgroud)

NSLog需要一个NSString参数,前面需要一个@符号.如果没有@符号,则字符串是普通的C字符串而不是NSString对象.