在iphone上使用复杂的字符串

时间:2010-10-25 05:44:13

标签: iphone objective-c

我想解析这个字符串

appDelegate.gpsArray= "\n\n 21.318\n03.863\n\n\n 21.317\n03.864\n\n\n 11.316\n113.864\n\n\n"

所以我无法获得价值 1)a[0]=21.318, a[1]=03.863,a[3]=11.317,a[4]=113.846....

或如何将其作为2d数组来表示 2)

a[0,0]=21.318,103.863

[1,1] = 21.318,103.863

我这样做但没有用

NSMutableArray  *a1 = [[NSMutableArray alloc] init];
        [a1 addObjectsFromArray:appDelegate.gpsArray   ];

请建议如何使用1D和2D两种方式

1 个答案:

答案 0 :(得分:1)

我会用这种方式解决你的问题:

NSArray *stringsArray = [appDelegate.gpsArray componentsSeparatedByString:"\n"];
NSMutableArray *result = [NSMutableArray array];

NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];

for (NSString *str in stringsArray ) {
    if( ![str isEqualToString:@""] ) {
        [result addObject:[f numberFromString:str]];
    }
}

[f release];

然后你可以将result数组转换为二维数组:

NSMutableArray *result2D = [NSMutableArray array];

for ( int i = 0; i < [result count] / 2; i++ ) {
    NSMutableArray *innerArray = [[NSMutableArray alloc] init];
    [innerArray addObject:[result objectAtIndex:2 * i]];
    [innerArray addObject:[result objectAtIndex:2 * i + 1]];
    [result2D addObject:innerArray];
    [innerArray release];
}