1. Code.
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // The first one is to modify the static global variable; the second one is to modify the variables modified with the new keyword __block. __block int blockLocal = 100; static int staticLocal = 100; void (^aBlock)(void) = ^(void){ blockLocal++; staticLocal++; }; NSLog(@"----blockLocal--%d",blockLocal); NSLog(@"---staticLocal--%d",staticLocal); aBlock(); NSLog(@"----blockLocal--%d",blockLocal); NSLog(@"---staticLocal--%d",staticLocal); }
2. Output.
2015-10-23 13:15:30.598 existBlockModify the value of the external variable[7561:195731] ----blockLocal--100 2015-10-23 13:15:30.598 existBlockModify the value of the external variable[7561:195731] ---staticLocal--100 2015-10-23 13:15:30.598 existBlockModify the value of the external variable[7561:195731] ----blockLocal--101 2015-10-23 13:15:30.598 existBlockModify the value of the external variable[7561:195731] ---staticLocal--101
Summarize
The above is the implementation code for modifying external variable values in Block introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!