苹果文档示例代码中的__block是什么意思?

问题描述 投票:1回答:1

在阅读NotificationCenter文档时,我发现了下面的示例代码,我想说明的是,这里的__block是什么意思?我想弄清楚的是,__block在这里是什么意思?我知道当使用__block时,block中的变量可以改变,但token并没有改变。

    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];
                                       }];

ios objective-c block
1个回答
2
投票

它允许使用 token 在初始化构造的块内,表示它的值以后会改变,所以可以在块中使用。

否则就会得到如下的结果。

demo

© www.soinside.com 2019 - 2024. All rights reserved.