拆分NSString不工作



我看过很多关于这个的话题,但是没有一个对我有用。我正在尝试解析GEORSS,坐标以以下格式给出:

<georss:point>4613618.31000115 676474.87007986</georss:point>

所以我试着把它分成一个NSArray,然后把它们分配给相应的XML键,但它总是崩溃的错误:

2013-04-02 12:33:11.234 GeoRss1[2125:c07] *由于未捕获异常'NSRangeException'而终止应用程序,原因:'* -[__NSArrayM objectAtIndex:]:索引1超出界限[0 ..]0) '*第一个抛出调用栈:

(0x1959012 0x1275e7e 0x18fb0b4 0x19d23a8 0x3f48 0xd55a37 0x1c96600 0x1ca1a2b 0x1ca0f07 0xd53e02 0x34b8 0xd7b589 0xd79652 0xd7a89a 0xd7960d 0xd79785 0xcc6a68 0x14a1911 0x14a0bb3 0x14df89da 0x18fb8fd 0x14df35da 0x18df2d5 0x13c9250 0x18dcf3f 0x18dc96f 0x18ff734 0x18fef44 0x18fe1b 0x18b37e3 0x18b3668 0x1b9ffc 0x1d05)libc + + abi。终止调用抛出异常(lldb)

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    }else if([elemAct isEqualToString:@"georss:point"]){
        componentes = [[string componentsSeparatedByString:@" "] mutableCopy];
        if ( componentes != nil){
            [xmlLat appendString:componentes[0]];
            [xmlLon appendString:componentes[1]];
        }
     ...
 ...

在该文件的头文件中,我正确地声明了两个变量并合成了它们:

...    
@property (strong) NSMutableString *xmlLat; 
@property (strong) NSMutableString *xmlLon;
...

谢谢大家的进步,你们已经帮助我在其他主题的一些回应!

我尝试了以下代码:

NSString *abc = @"4613618.31000115 676474.87007986";
NSArray *componentes = [[abc componentsSeparatedByString:@" "] mutableCopy];
NSLog(@"componentes : %@",componentes);
NSMutableString *xmlLat = [[NSMutableString alloc]initWithString:@""];
NSMutableString *xmlLon = [[NSMutableString alloc]initWithString:@""];
[xmlLat appendString:componentes[0]];
[xmlLon appendString:componentes[1]];
NSLog(@"xmlLat : %@",xmlLat);
NSLog(@"xmlLon : %@",xmlLon);

对我来说效果很好。我认为问题是你没有得到适当的数据在你的字符串

最新更新