Blocks式の構文

1124 ワード

Block Literal Syntaxについて簡単に説明します.Blockの出現はObjective Cにおける匿名関数の問題を解決するためです.匿名関数を使用すると、プログラムをより簡潔にし、理解しやすくし、プログラマーの関数命名作業を減らすことができます.
1.Block構文
^戻り値タイプパラメータリスト式
^int (int count){return count + 1;}

^パラメータリスト式
^(int count){ return count+ 1;}

戻り値タイプかintか
^式
^void (void) { printf("Blocks
";}

戻り値タイプとパラメータリストがないので省略できます
^{printf("Blocks
";}

2.Block型変数
typedef int (^blk_t)(int);
typedef int(^SumBlockT)(int a,int b)
typedef void(^blockname)(NSDictionary* dict, NSError* error)

3.Block内付与、_Block
__weak __typeof(self)weakSelf = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [weakSelf.delegate preOperateInBackgroundAtPageIndex:[self switchToPageIndexForIndex:index] ofBroadcastView:weakSelf];
        });