2014-04-08 20 views
1

到坐标我有一些坐标,我转换成用下面的代码字符串:将字符串转换在Objective-C

label3.text = [NSString stringWithFormat:@"%f%f", 
       venue.location.coordinate.latitude, 
       venue.location.coordinate.longitude]; 

不过,我现在想的字符串转换回纬度和经度坐标。我该怎么做呢?

+2

如果你在两个数字之间加上一个分隔符,它会有帮助。 – rmaddy

+0

几乎肯定比字符串解析更容易重新访问信息的原始来源。 –

回答

1

你不能那样做,因为你的代码没有在值之间提供分隔符。因此,你将无法一个字符串

12.3456.78 

已经低于对一个要弄清楚:

12.3 456.78 
12.34 56.78 
12.345 6.78 
12.3456 .78 

为了使转换可逆的,使用@"%f %f"格式(带空间)。如果你用分隔符分开你的花车,这样

float lat, lon; 
sscanf([label3.text UTF8String], "%f %f", &lat, &lon); 
+0

这工作完美。非常感谢,花了几个小时试图弄清楚。 – user3429966

0

label3.text = [NSString stringWithFormat:@"%f;%f", 
       venue.location.coordinate.latitude, 
       venue.location.coordinate.longitude]; 

然后

例如, sscanf - 现在你可以使用解析数据从字符串后面的许多不同的方式
CGPoint yourPoint=CGPointMake((NSString*)([[label3.text componentsSeparatedByString:@";"] 
firstObject]).floatValue,(NSString*)([[label3.text componentsSeparatedByString:@";"] lastObject]).floatValue) 
//yourPoint.x is latitude, yourPoint.y is longitude 
+1

CLLocationCoordinate2D将是比CGPoint更合适的结构。事实上,OP应该保持这样一个结构中的坐标,而不是从字符串中删除。 – Anna