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

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

    It seem as you are performing an asynchronous operation and when the completion block is called you have already left the function/scope where s was defined.

    consider declaring s in a "global" scope (like a class variable or property) then as long as your class instance is alive, you will be able to read the results set by the block.

    I would recommend using a property, and capturing a "weak self" pointer in the block, in order to avoid bad memory access in case that the instance holding s was deallocated.

提交回复
热议问题