代码之家  ›  专栏  ›  技术社区  ›  user6631314

IOS/Objective-C:用于调用完成bloc的语法

  •  -1
  • user6631314  · 技术社区  · 7 年前

    -(void)getMyBlock: (void (^)(NSString *))completed; // It returns a string.
    

    以下尝试(以及我尝试过的其他十种尝试)称之为无效:

    [self getMyBlock: (void (^)(NSString*)completed];
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   Glenn Posadas    7 年前

    块方法应该是这样的:

    -(void)getMyBlock: (void (^)(NSString * yourString))completed {
        completed(@"HELLO");
    }
    

    你这样称呼它:

    [self getMyBlock:^(NSString *yourString) {
        NSLog(@"%@", yourString);
    }];
    

    http://goshdarnblocksyntax.com/

        2
  •  2
  •   rmaddy    7 年前

    你需要提供一个街区 completed

    [self getMyBlock:^(NSString *result) {
        // this code is called when the asynchronous code inside getMyBlock is done
    }];
    

    在执行 getMyBlock 方法代码如下:

    - (void)getMyBlock: (void (^)(NSString *))completed {
        // do some asynchronous stuff
        NSString *result = // some result string
        if (completed) {
            completed(result);
        }
    }