LoginSignup
12
13

More than 5 years have passed since last update.

Blocksの記述パターン

Last updated at Posted at 2013-09-13

1.typedef (戻り値)(^ブロック型名)(引数)

typedef void(^Test)(NSString*);
Test test = ^void (NSString* str) {// リターンのvoidは省略可
    if(str) {
        NSLog(@"ON");
    }
    else {
        NSLog(@"OFF");
    }
}

typedefを使うとxcodeでコード補完ができない気がする。。。

2.プロパティでのBlock

@property(copy, nonatomic) void (^test)(NSString*);

3.引数でのBlock

-(void)blockTest:(void (^)(Test*)) testBlock {

}

4.戻り値でのBlock

-(void (^)(Test*))testBlock {
    void (^testBlock) (Test*) = ^(Test* test) {
        test.score = 100;
    };

    return testBlock;
};
12
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
13