我想在数组/字典中找到类似的字符串,并通过在字符串中添加后缀来重命名它们。例如,假设我的数组/字典包含如下字符串:
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)
答案 0 :(得分: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]];
}
}
祝你好运