对于block对self的操作:
//不能单独在block块内使用self
void (^incorrectBlockObject)(void) = ^{
NSLog(@"self = %@", self); /* self is undefined here */
};
//可以通过传递参数的方式使用self
void (^correctBlockObject)(id) = ^(id self){
NSLog(@"self = %@", self);
};
- (void) callCorrectBlockObject{
correctBlockObject(self);
}
//方法内部的block块可以单独使用self
- (void) simpleMethod{
NSMutableArray *array = [[NSMutableArray alloc]
initWithObjects:@"obj1",
@"obj2", nil];
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSLog(@"self = %@", self);
/* Return value for our block object */
return NSOrderedSame; }];
}
对block中的点语法
//可以在方法内部的block块中使用点语法
#import "AppDelegate.h"
@interface AppDelegate()
@property (nonatomic, copy) NSString *stringProperty;
@end
@implementation AppDelegate
- (void) simpleMethod{
NSMutableArray *array = [[NSMutableArray alloc]
initWithObjects:@"obj1",
@"obj2", nil];
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSLog(@"self = %@", self);
self.stringProperty = @"Block Objects";
NSLog(@"String property = %@", self.stringProperty);
/* Return value for our block object */
return NSOrderedSame; }];
}
//但是在单独block块中只能使用[self propertyXXX]
void (^incorrectBlockObject)(id) = ^(id self){
NSLog(@"self = %@", self);
/* Should use setter method instead of this */
self.stringProperty = @"Block Objects";
/* Compile-time Error */
/* Should use getter method instead of this */
NSLog(@"self.stringProperty = %@", self.stringProperty);
/* Compile-time Error */
};
//使用[self propertyXXX]代替
void (^correctBlockObject)(id) = ^(id self){
NSLog(@"self = %@", self);
/* This will work fine */
[self setStringProperty:@"Block Objects"];
/* This will work fine as well */
NSLog(@"self.stringProperty = %@",
[self stringProperty]);
};
block块会对变量进行copy操作
typedef void (^BlockWithNoParams)(void);
- (void) scopeTest{
NSUInteger integerValue = 10;
BlockWithNoParams myBlock = ^{
NSLog(@"Integer value inside the block = %lu",
(unsigned long)integerValue);
};
integerValue = 20;
/* Call the block here after changing the
value of the integerValue variable */
myBlock();
NSLog(@"Integer value outside the block = %lu",(unsigned long)integerValue);
}
//输出:
/*
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value inside the block = 10
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value outside the block = 20
*/
//使用__block
typedef void (^BlockWithNoParams)(void);
- (void) scopeTest{
__block NSUInteger integerValue = 10; //加入block前缀
BlockWithNoParams myBlock = ^{
NSLog(@"Integer value inside the block = %lu",
(unsigned long)integerValue);
};
integerValue = 20;
/* Call the block here after changing the
value of the integerValue variable */
myBlock();
NSLog(@"Integer value outside the block = %lu",(unsigned long)integerValue);
}
//输出:
/*
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value inside the block = 20
2015-03-31 11:55:57.557 testBlock[20096:527288] Integer value outside the block = 20
*/
若要在block块内改变变量的值,也要使用__block
- (void)simpleMethod{
__block NSUInteger outsideVariable = 10;
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:@[@2,@4,@3]];
NSArray * array2 = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSUInteger insideVariable = 20;
outsideVariable = 50;
NSLog(@"Outside variable is %ld", (long)outsideVariable);
NSLog(@"Inside variable is %ld", (long)insideVariable);
}
//输出:
/*
2015-03-31 11:55:57.556 testBlock[20096:527288] Outside variable is 50
2015-03-31 11:55:57.556 testBlock[20096:527288] Inside variable is 20
*/