1

When we need to call self in a block, we usually do as this:

__typeof(&*self) __weak weakSelf = self;

The question is, when you call weakSelf.prop = @"string" in block, the setter setProp: will never be called.

But if you define like this:

__typeof(&*self) fakeSelf = self;

The problem solved.

Anyone could explain why? And any potential problem?

nickcheng
  • 516
  • 4
  • 22
  • 1
    First off, `&*` is redundant (the address of the pointer dereferencing something is just something). Second, if it isn't being called, then your class is being deallocated before it's able to evaluate in the block. – CodaFi May 22 '13 at 13:37
  • 1
    @CodaFi The `&*` once served to remove the implicit `__strong` from the type of self. It's not needed any more, though. – Nikolai Ruhe May 22 '13 at 13:41
  • 1
    "When we need to call self in a block, we usually do as this:" No, "we" don't "usually do it like this". We do this only when we see that there would be a specific problem (retain cycle) in the particular way we use it. In many coding patterns, retaining `self` by a block is necessary for it to work correctly. – newacct May 22 '13 at 22:18

1 Answers1

5
  1. typeof is part of C11 and supported as an extension in older language version by clang. No need to use (implementation private) __typeof
  2. No &*self hackery needed any more. clang supports the plain typeof(self) since a while (v3.1, Xcode 4.4).
  3. weakself.prop = ... alway is a synonym for [weakself setProp:...]. This does not change when declaring the target __weak.

How do you come to the conclusion the accessor would not be called?

If you miss a log message or breakpoint hit maybe the target weakSelf was already deallocated and the variable was set to nil. This would of course prevent the accessor from being called.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200