1

我已经尝试了所有我能想到的方法,但就是没有得到它。

我有一个 XML 文件,其中包含这样的条目......

<lat>53.358242</lat>
<lon>-6.227800</lon>

该文件使用 NSXMLParser 解析并取出其他内容,但无法将这些内容输入注释以删除引脚。在 .h 文件中,我有以下内容;

@interface LocationList : NSObject
@property (nonatomic, strong) NSString *loc_name;
@property (nonatomic, strong) NSString *lat;
@property (nonatomic, strong) NSString *lon;

然后我尝试将 NSString 转换为坐标。

CLLocationCoordinate2D pinCoordinate;
pinCoordinate.latitude = [theLocationList.lat doubleValue]; 
pinCoordinate.longitude = [theLocationList.lon doubleValue]; 

但我最终得到0。

我试过这个,但它无法处理负值。

NSString *digits = [theLocationList.lon stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSLog(@"Attempts: %f", [digits floatValue]);

有谁能帮我解惑?

4

1 回答 1

1

对您的解决方案稍作改动应该可以解决问题 - 替换decimalDigitCharacterSetcharacterSetWithCharactersInString:@"+-0123456789.",如下所示:

NSString *digits = [theLocationList.lon stringByTrimmingCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"+-0123456789."] invertedSet]];
NSLog(@"Attempts: %f", [digits floatValue]);
于 2012-06-27T15:01:12.457 回答