转自: http://fei263.blog.163.com/blog/static/9279372420113193523828/
^(傳入 參數列 ) { 行為主體 };
^( int a) { return a*a;};這是代表Block會回傳輸入值的平方值( int a 就是 參數列 , return a*a; 就是 行為主體 )。記得主體裡最後要加";"因為是敘述,而整個{}最後也要要加";"因為Block是個物件實體。
int result = ^(int a) {return a*a ;} (5) ;很怪吧。後面小括號裡的5 會被當成a的輸入值然後經由Block輸出5*5 = 25指定給result這個變數。
回傳值 (^名字 ) (參數列 );
int (^ square) (int);// 有一個叫 square 的 Block Pointer ,其所指向的Block是有一個int 輸入和 int 輸出
square = ^(int a ) {return a*a ;}; // 將剛剛Block 實體指定給 square
int result = square(5); // 感覺上不就是funtion的用法嗎?也可以把Block Pointer當成參數傳給一個function,比如說
void myFuction( int (^mySquare) (int) ); // function 的宣告,傳入一個有一個int輸入和int輸出的Block 型別的參數
int (^mySqaure) (int) = ^(int a) {return a*a;};
// 先給好一個有實體的block pointer叫mySquare
myFunction( mySqaure ) ; //把mySquare這個block pointer給myFunction這個function或是不用block pointer 直接給一個block 實體,就這樣寫
myFunction( ^(int a) {return a*a} ) ;當成Objective-C method 的傳入值的話都是要把型別寫在變數前面然後加上小括號,因些應該就要這樣寫
-(void) objcMethod:( int (^) (int) ) square; // square 變數的型別是 int (^) (int)讀文至此是不是對Block有基本的認識? 接下來我們要談談Block相關的行為和特色
{我們再來看一個很有趣的例子
int outA = 8;
int (^myPtr) (int) = ^(int a) {return outA+a;};
// block 裡面可以讀同一個scope的outA的值
int result = myPtr(3); // result is 11
}
{事實上呢,myPtr在其主體用到outA這個變數值的時候是做了一個copy的動作把outA的值copy下來。所以之後outA即使換了新的值對於myPtr裡copy的值是沒有影響到的。
int outA = 8;
int (^myPtr) (int) = ^(int a) {return outA+a;};
// block 裡面可以讀同一個scope的outA的值
outA = 5; // 在呼叫myPtr之前改變outA的值
int result = myPtr(3); // result 的值還是 11並不是 8
}
{原本mutableArray的值是{@"one",@"two",@"three"}在block裡被更改mutableArray所指向的物件後,mutableArray的值就會被成{@"one",@"two"}
NSMutableArray * mutableArray = [NSMutableArray arrayWithObjects:@"one",@"two",@"three",nil];
int result = ^(int a) { [mutableArray removeLastObject]; return a*a;} (5);
NSLog(@"test array %@", mutableArray);
}
{甚至可以在block裡面直接改變outA的值比如這樣寫
static int outA = 8;
int (^myPtr) (int) = ^(int a) {return outA+a;};
// block 裡面可以讀同一個scope的outA的值
outA = 5; // 在呼叫myPtr之前改變outA的值
int result = myPtr(3); // result 的值是 8,因為outA是個static 變數會直接反應其值
}
{3. Block Variable
static int outA = 8;
int (^myPtr) (int) = ^(int a) { outA= 5; return outA+a;};
// block 裡面改變outA的值
int result = myPtr(3); // result 的值是 8,因為outA是個static 變數會直接反應其值
}
{因為myPtr和myPtr2都有用到num這個block variable,最後result的值就會是7
__block int num = 5;
int (^myPtr) (int) = ^(int a) { return num++;};
int (^myPtr2) (int) = ^(int a) { return num++;};
int result = myPtr(0);
result = myPtr2(0);
}
typedef int (^MyBlock)(int);此程式由genBlock裡產生的block再指定給main function的 outBlock 變數,執行這個程式會得到
MyBlock genBlock();
int main(){
MyBlock outBlock = genBlock();
int result = outBlock(5);
NSLog(@"result is %d",[outBlock retainCount] ); // segmentation fault
NSLog(@"result is %d",result );
return 0 ;
}
MyBlock genBlock() {
int a = 3;
MyBlock inBlock = ^(int n) {
return n*a;
};
return inBlock ;
}
MyBlock genBlock() {這樣[inBlock copy]的回傳值就會被放到heap,就可以一直使用(記得要release)
int a = 3;
MyBlock inBlock = ^(int n) {
return n*a;
};
return [inBlock copy] ;
}
MyBlock genBlock() {-copy指令是為了要把block 從stack搬到heap,autorelease是為了平衝retainCount加到autorelease oop ,回傳之後等到事件結束就清掉。
int a = 3;
MyBlock inBlock = ^(int n) {
return n*a;
};
return [[inBlock copy] autorelease] ;
}
MyBlock genBlock() {結果會印出
int a = 3;
NSMutableString * myString = [NSMutableString string];
MyBlock inBlock = ^(int n) {
NSLog(@"retain count of string %d",[myString retainCount]);
return n*a;
};
return [inBlock copy] ;
}
MyBlock genBlock() {執行的結果就是會
int a = 3;
__block NSMutableString * myString = [NSMutableString string];
MyBlock inBlock = ^(int n) {
NSLog(@"retain count of string %d",[myString retainCount]);
return n*a;
};
return [inBlock copy] ;
}
@interface MyObject : NSObject {
NSString * title;
void (^ myLog) (NSString * deco);
}
-(void) logName;
@end
@implementation MyObject
-(id) initWithTitle:(NSString * ) newTitle{
if(self = [super init]){
title = newTitle;
myLog = [^(NSString * deco) { NSLog(@" %@%@%@",deco, title, deco ); } copy];
}
return self;
}
-(void) logName{
myLog(@"==");
}
-(void ) dealloc{
[myLog release];
[title release];在main 裡使用如下
[super dealloc];
}
@end
-(id) initWithTitle:(NSString * ) newTitle{在Block主體裡用newTitle這個變數而不是title。這樣self就不會被retain了。
if(self = [super init]){
title = newTitle;
myLog = [^(NSString * deco) { NSLog(@" %@%@%@",deco, newTitle, deco ); } copy];
}
return self;
}