Is self retained within this Objective-C block?

前端 未结 1 526
长情又很酷
长情又很酷 2021-02-10 03:24

When I have a block in Objective-C that looks like this:

self.request = [[ASIHTTPRequest requestWithURL:...

[self.longPollRequest setCompletionBlock:^{
    NSLo         


        
1条回答
  •  别跟我提以往
    2021-02-10 04:07

    As the Block Programming Topics says:

    In a reference-counted environment, by default when you reference an Objective-C object within a block, it is retained. This is true even if you simply reference an instance variable of the object. Object variables marked with the __block storage type modifier, however, are not retained.

    If you use a block within the implementation of a method, the rules for memory management of object instance variables are more subtle:

    If you access an instance variable by reference, self is retained;

    If you access an instance variable by value, the variable is retained.

    you reference self in block, so self is retained.

    0 讨论(0)
提交回复
热议问题