Apple 文档示例代码中的 __block 是什么意思?
what does __block mean in Apple doc sample code?
在阅读NotificationCenter文档时,我找到了下面的示例代码。我想澄清的是这里的 __block 是什么意思?我知道什么时候使用 __block 变量可以在块中更改,但令牌不会更改。
NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
id __block token = [center addObserverForName:@"OneTimeNotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog(@"Received the notification!");
[center removeObserver:token];
}];
允许在初始化构造的block内使用token
,说明后面会改变它的值,所以可以在block内使用。
否则你会得到如下结果。
在阅读NotificationCenter文档时,我找到了下面的示例代码。我想澄清的是这里的 __block 是什么意思?我知道什么时候使用 __block 变量可以在块中更改,但令牌不会更改。
NSNotificationCenter * __weak center = [NSNotificationCenter defaultCenter];
id __block token = [center addObserverForName:@"OneTimeNotification"
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {
NSLog(@"Received the notification!");
[center removeObserver:token];
}];
允许在初始化构造的block内使用token
,说明后面会改变它的值,所以可以在block内使用。
否则你会得到如下结果。