0

Why new Xcode versions gives a warning about using self inside a block.

Warning:

Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

@interface ViewController : UIViewController {
     NSString *myString;
}

And the warning hides when we prefix variable name with self->.

I read there is no difference between directly calling variable & self-> in rmaddy's answer

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
  • Check the last screenshot: https://stackoverflow.com/a/49028910/1801544 It was either not implemented on previous versions of XCode, or was set by default to NO? – Larme Aug 07 '18 at 13:06
  • But the older xcode shows warning to access property values. If define variable in `.m` file, older xcode doesn't show warning. – Lal Krishna Aug 07 '18 at 13:12
  • But what's the LLVM version? Could be a different parsing/checking in the XCode new version. – Larme Aug 07 '18 at 13:21
  • Apple LLVM 9.0. Yes. As @knightIdDragon's answer, it gives us clear warning about retaining – Lal Krishna Aug 07 '18 at 13:23

1 Answers1

2

This is actually a good warning to have, so I can see them flipping the flag. More often than not, people are accidently retaining self in blocks, creating retain cycles. This warning informs you the developer that you may have a possible retain cycle happening.

Basically, if you see the self then you know the block is retaining it, where if you do not see the self, you may be inclined at first glance to think you are using a local variable.

Knight0fDragon
  • 16,609
  • 2
  • 23
  • 44