No.1 整型變量 int x = 10; void (^vv)(void) = ^{printf("x is %d\n", x);}; x = 11; vv(); 運行結果:x is 10 No.2 字符變量 NSString* str = @"123"; void (^vv)(void) = ^(void){NSLog(@"str is %@", str);}; str = @"456"; vv(); 運行結果:str is 123 No.3 類成員的整型變量 @interface Test : NSObject { int x; } @property (nonatomic, assign) int x; @end @implementation Test @end Test* t = [[Test alloc] init]; t.x = 10; void (^vv)(void) = ^(void){NSLog(@"t is %d", t.x);}; t.x = 20; vv(); 運行結果:t is 20 No.4 類成員的字符變量 - (void)logInfo { self.name = @"123"; void (^test)(void) = ^{NSLog(@"%@", self.name);}; self.name = @"456"; test(); } Person* p = [[Person alloc] init]; p.name = @"1111"; void (^vv)(void) = ^(void){NSLog(@"t is %@\n", p.name);}; p.name = @"2222"; vv(); [p logInfo]; 運行結果:t is 2222 456 今日和前輩一塊兒研究後發現: block在捕獲變量的時候只會保存變量被捕獲時的狀態(對象變量除外), 以後即使變量再次改變,block中的值也不會發生改變 同時,形如如下對常量的賦值在Xcode環境下 NSString* str = @"123"; NSLog(@"p1 = %p", str); void (^vv)(void) = ^(void){NSLog(@"str is %@", str);}; str = @"456"; NSLog(@"p2 = %p", str); vv(); p1和p2打印出指針的值是不同的