【发布时间】:2023-03-29 23:29:01
【问题描述】:
我有以下错误
-[__NSPlaceholderArray initWithObjects:count:]: 尝试从 objects[1539] 中插入 nil 对象
有时会在屏幕上点击几次,因为代码很少,所以所有代码都粘贴在下面
@interface ViewController ()
@property (nonatomic,weak) NSTimer *timer;
@property (nonatomic,strong)NSMutableArray * testArray;
@property (nonatomic,strong) dispatch_queue_t queue1;
@property (nonatomic,strong) dispatch_queue_t queue2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.testArray = [NSMutableArray array];
_queue1 = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
_queue2 = dispatch_queue_create("test",DISPATCH_QUEUE_SERIAL);
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(addObjectforArray) userInfo:nil repeats:YES];
[timer fire];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
dispatch_async(_queue2, ^{
NSLog(@"touchesBeganThread:%@",[NSThread currentThread]);
NSArray * testTempArray = [NSArray arrayWithArray:self.testArray];
for (UIView *view in testTempArray) {
NSLog(@"%@",view);
}
});
}
- (void)addObjectforArray{
dispatch_async(_queue1, ^{
NSLog(@"addObjectThread:%@",[NSThread currentThread]);
[self.testArray addObject:[[UIView alloc]init]];
});
}
我不明白为什么会这样,如果我把_queue1改为DISPATCH_QUEUE_SERIAL,就正常了。
我该如何理解这个问题?如果有人能提供一些启示,那就太好了。
【问题讨论】:
标签: ios objective-c crash