Search code examples
iosobjective-cblock

what does __block mean in Apple doc sample code?


When read NotificationCenter document, I found the sample code below. What I want to clarify is what does __block mean here? I know when using __block variable can change in block, but token doesn't changed.

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


Solution

  • It allows to use token within block of initialization construction, indicating that its value will be changed later, so can be used in block.

    Otherwise you get as below.

    demo