0

我想在数组/字典中找到类似的字符串,并通过在字符串中添加后缀来重命名它们。例如假设我的数组/字典包含这样的字符串:

1.Work
2.Work
3.Work
4.Home
5.Home
6.Mobile
7.Mobile 

等可能有任何类似的字符串。现在我想像这样重命名它们:

1.Work(1)
2.Work(2)
3.Work(3)
4.Home(1)
5.Home(2)
6.Mobile(1)
7.Mobile(2)
4

1 回答 1

1

好吧,我不知道字典,但是对于一个数组,你可以做这样的事情。

    NSMutableArray *myArr = [fill the array with w/e];
    int home;
    int mobile;
    int work;
    for(int x = 0; x < [myArr count]; x++){
    NSString *r = [myArr objectAtIndex:(NSUInteger)x];
    if([r isEqualToString:@"work"]){
    work++;
    [myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:@"%@(%i)", r, work]];
    }
    else if([r isEqualToString:@"home"]){
    home++;
    [myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:@"%@(%i)", r, home]];
    }
    else if([r isEqualToString:@"mobile"]){
    mobile++;

    [myArr replaceObjectAtIndex:x withObject:[NSString strinWithFormat:@"%@(%i)", r, mobile]];

    }
    }

祝你好运

于 2011-06-09T05:29:45.513 回答