零對象我有這樣的提示以下錯誤:嘗試插入從對象
- [__ NSPlaceholderArray initWithObjects:數:]:嘗試從物體[1539]
插入零對象有時會發生我試圖挖掘在屏幕上數倍,因爲代碼是很少,所以所有的代碼粘貼下面
@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,它變得正常。
我該如何理解這個問題?如果有人能夠說出一些光芒,那將是美好的。
' - [UIView init]'永遠不會返回'nil'。 –
你需要分配它。 [[UIView alloc] init] – Nirmalsinh