代码如下:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"acaas",@"acaas",@"jacvn",@"caocamo",@"heima",@"acaas", nil];
[arr insertObject:@"heima" atIndex:1];
for (int i = 0; i < arr.count - 1; i++)
{
for (int j = i+1;j < arr.count; j++)
{
if ([arr[i] isEqualToString: arr[j]])
{
[arr removeObjectAtIndex:j];
// 当删除NSMutableArray中索引为j的元素之后,其后的元素会自动向前进行补充,所以需要把当前索引减1,才能保证完整遍历数组中的元素
j--;
}
}
}
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj);
}];
return 0;
} |
|