美文网首页
ios - 删除数组中某个元素

ios - 删除数组中某个元素

作者: lizhi_boy | 来源:发表于2019-06-06 14:18 被阅读0次
  • 删除数组中某个元素
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"1",@"2",@"3",@"1",@"5", nil];
    for (int i=0; i<array.count; i++) {
        NSString *str = array[i];
        if ([str isEqualToString:@"1"]) {
            [array removeObjectAtIndex:i];
            i--;
        }
    }
    NSLog(@"%@",array);
  • 数组去重
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"1",@"1",@"1",@"2",@"2",@"3",@"1",@"2",@"2",@"5",@"8888", nil];
    for (int i=0; i<array.count; i++) {
        NSString *str1 = array[i];
        for (int j=i+1; j<array.count; j++) {
            NSString *str2 = array[j];
            if ([str2 isEqualToString:str1]) {
                [array removeObjectAtIndex:j];
                j--;
            }
        }
    }
    NSLog(@"%@",array);

相关文章

网友评论

      本文标题:ios - 删除数组中某个元素

      本文链接:https://www.haomeiwen.com/subject/inbyxctx.html