如何在iPhone中从字符串中分离对象和值?

时间:2014-01-28 13:07:37

标签: objective-c

[[Beverages:beverages.png], [Dairy:dairy.png], [Grab and Go:grabandgo.png], [Grocery:grocery.png], [Non Food Products:non-food.png], [Specialty Shop:specialitems.png], [Proteins:protein.png]]

=============================================== ===================================

上面是String。

我希望它们像个人一样分开,将它们添加到数组中,如

[Beverages,Non Food Products,.....]

[grocery.png,dairy.png,.......]

1 个答案:

答案 0 :(得分:1)

尝试以下

NSMutableArray *firstArray, *secondArray;
firstArray=[[NSMutableArray alloc] init];
secondArray=[[NSMutableArray alloc] init];

NSString *str=@"[[Beverages:beverages.png], [Dairy:dairy.png], [Grab and Go:grabandgo.png], [Grocery:grocery.png], [Non Food Products:non-food.png], [Specialty Shop:specialitems.png], [Proteins:protein.png]]";
NSArray *arrComa=[str componentsSeparatedByString:@","];

for(NSString *strSub in arrComa)
{
    NSArray *arrColon=[strSub componentsSeparatedByString:@":"];
    NSString *firstString=[(NSString *)[arrColon objectAtIndex:0] stringByReplacingOccurrencesOfString:@"[" withString:@""];
    NSString *secondString=[(NSString *)[arrColon objectAtIndex:1] stringByReplacingOccurrencesOfString:@"]" withString:@""];
    [firstArray addObject:firstString];
    [secondArray addObject:secondString];
}
NSLog(@"%@", firstArray);
NSLog(@"%@", secondArray);