-1

我从 GPS 获取坐标,需要构建一个如下所示的小字符串:

      NSString *location = @"40.7,-73.9";

我现在的代码是

       NSString *latitude = [NSString stringWithFormat:@"%f", [LocationFinder    
                                singleton].location.coordinate.latitude];
       NSString *longtitude = [NSString stringWithFormat:@"%f", [LocationFinder 
                                singleton].location.coordinate.latitude];
       // Of course this doesn;t work
       NSString *location = latitude, logtitude;

现在我知道我可以执行 NSMutablestring 和追加,但我只是想知道是否有一个非常有效的方法来执行此过程。来自 .NET 和 Java,我讨厌 Obj C,因为它有这种怪异之处。

4

3 回答 3

4

最简单的答案可能是这里最好的答案,它+stringWithFormat:与实际的多部分格式字符串一起使用,而不是像你在那里那样单独使用:

Coordinate *coord = [LocationFinder singleton].location.coordinate; // or whatever the type here is
NSString *location = [NSString stringWithFormat:@"%f,%f", coord.latitude, coord.longitude];

但总的来说,您的问题是毫无意义的微优化的一个很好的例子。

于 2013-10-07T01:31:14.610 回答
1

为什么不是这个?

NSString *location = [NSString stringWithFormat:@"%f,%f", [LocationFinder    
                            singleton].location.coordinate.latitude, [LocationFinder 
                            singleton].location.coordinate.longitude]
于 2013-10-07T01:31:27.567 回答
0

您正在加载latitude经度值。

要获得这样的结果:@"40.7,-73.9"需要设置 s 格式的小数位数float

NSString *location = [NSString stringWithFormat:@"%.01f,%.01f",
                        [LocationFinder singleton].location.coordinate.latitude,
                        [LocationFinder singleton].location.coordinate.longitude];
于 2013-10-07T03:40:18.347 回答