2012年1月17日火曜日

ブロックについて

ブロック=無名関数
その場限りの名無しの関数
ブロックで書かれる処理のreturnは一個しか書けない

ブロックの書き方
^(仮引数){実行する処理}
ex)
^(id num1 id num2){
if(num1>num2){
res = num1;
}else{
res = num2;
}
return res;
}
書き方2
void (^変数名)(引数)=^(仮引数){処理}


NSNumber (^block)(id,id) = ^(id num1, id num2){
NSnumber result;
if(num1 > num2){
result = num1;
}else{
result = num2;
}
return result;

のような書き方がある
"^"は、関数のポインタを示す。

No response to “ブロックについて”

Leave a Reply