//联系人:石虎QQ:1224614774昵称:嗡嘛呢叭咪哄
一、概念
1.指出以下这段代码的问题
- (void)test{
CGRectframe =CGRectMake(20,200,200,20);
self.alert = [[UILabel alloc]initWithFrame:frame];
self.alert.text =@"Please wait 10 seconds...";
self.alert.textColor = [UIColor redColor];
[self.view addSubview:self.alert];
NSOperationQueue*waitQueue = [[NSOperationQueuealloc]init];
[waitQueueaddOperationWithBlock:^{
[NSThreadsleepUntilDate:[NSDatedateWithTimeIntervalSinceNow:10]];
self.alert.text =@"石虎";
NSLog(@"执行一个新的操作,线程:%@", [NSThread currentThread]);
}];
}
正确修改为:
[waitQueueaddOperationWithBlock:^{
[NSThreadsleepUntilDate:[NSDatedateWithTimeIntervalSinceNow:10]];
//正确要在主线程赋
dispatch_async(dispatch_get_main_queue(), ^{
self.alert.text =@"石虎";
});
NSLog(@"执行一个新的操作,线程:%@", [NSThread currentThread]);
}];
2.写出下列程序的输出结果
typedefvoid(^BoringBlock)(void);
intmain(intargc,constchar*argv[])
{
inta =23;
__blockintb =23;
BoringBlock block1 = ^{NSLog(@"a1==%d",a);};
BoringBlock block2 = ^{NSLog(@"b1==%d",b);};
a =32;
b =32;
BoringBlock block3 = ^{NSLog(@"a1==%d",a);};
BoringBlock block4 = ^{NSLog(@"b2==%d",b);};
block1();
block2();
block3();
block4();
return0;
}
打印结果:
2017-10-3020:29:25.645石虎测试 demo[969:6507971] a1==23
2017-10-3020:29:25.646石虎测试 demo[969:6507971] b1==32
2017-10-3020:29:25.646石虎测试 demo[969:6507971] a1==32
2017-10-3020:29:25.646石虎测试 demo[969:6507971] b2==32