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

前端 未结 4 1955
小鲜肉
小鲜肉 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:09

    Is not totally clear why and what you want to do so I give you 3 options:

    • Declare the variable s outside the block, it will be copied inside automatically inside the block
    • If you need to change its value and read it declare it outside the block with the __block specifier, pay attention that if you are changing that var in an async process, the value after the block will be most probably useless
    • If you want to keep and change its value during each call to the block decalre it inside the block as static put it will be "visible" only inside the block

提交回复
热议问题