How can I retain a local variable that is set in a block?

前端 未结 4 1965
小鲜肉
小鲜肉 2021-01-26 19:56

So I have:

@interface testAppControl : NSObject
{
    NSString *s;
}

and then in my block I want to do

[SendAPI setGroupWithNam         


        
4条回答
  •  感动是毒
    2021-01-26 20:15

    Declare it as a __block variable, then you can use it outside the block and alter it inside the block.

    __block NSString *s = nil;
    
    void(^block)(void) = ^ {
        s = @"something";
    };
    block();
    NSLog(@"%@", s);
    
    s = @"blah";
    NSLog(@"%@", s);
    
    block();
    NSLog(@"%@", s);
    

提交回复
热议问题