例:从一个可变数组中移除小于10的元素,假如这个数组中的元素为2,6,8,4,12,13,10,15,16
@interface ViewController ()
/** 临时数组 */
@property (nonatomic,strong)NSMutableArray *tempArr;
/** 数组 */
@property (nonatomic,strong)NSMutableArray *nums;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (NSMutableArray *)tempArr {
if (!_tempArr) {
_tempArr = [NSMutableArray array];
}
return _tempArr;
}
- (NSMutableArray *)nums {
if (!_nums) {
_nums = [NSMutableArray array];
}
return _nums;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.nums = [NSMutableArray arrayWithObjects:@2,@6,@8,@4,@12,@13,@10,@15,@16, nil];
self.tempArr = [_nums mutableCopy];
NSLog(@"%@",_tempArr);
for (NSNumber *num in _nums) {
if ([num integerValue] < 10) {
// 一个数组不能同时有一个线程去读取,然后另个线程去写,所以出错
// 解决方法:新建temp Array ,再做处理!
// [self.nums removeObject:num];
[self.tempArr removeObject:num];
}
}
NSLog(@"%@",_tempArr);
}