1

I have a block that uses a weak reference of itself to access properties inside the block. When accessing those properties, I use

__weak ViewController *weakSelf = self;
someBlock = ^{

ViewController *safeSelf = weakSelf;

weakSelf.someObject
safeSelf->someObject

}

When using weakself, what is the reason for using dot syntax and for the strong reference from the weak reference, to use -> syntax

Tony
  • 656
  • 8
  • 20
  • 1
    There is no reason to use `->` for either reference. – trojanfoe May 14 '14 at 14:26
  • @CrimsonChris No, it's not a duplicate of that. The concern here has only to do with how to address the reference to `self` _in the weak-strong dance_. – matt May 14 '14 at 14:30
  • Check out this [link](http://aceontech.com/objc/ios/2014/01/10/weakify-a-more-elegant-solution-to-weakself.html) for a more elegant approach to the _weak-strong dance_. – CrimsonChris May 14 '14 at 14:55

3 Answers3

2

The object->iVar syntax accesses the instance variable directly, without using the property.

You should forget you ever saw this, and never, ever use it (Until you get to the point where you understand this stuff cold, and find the .01% edge case where you need it.)

That syntax allows you to reach into another object and access it's instance variables directly, which is bad practice. Properties allow you to control access to an object's public interface, and maintain the encapsulation of the object.

The __weak weakSelf convention is for code blocks. Code blocks capture a strong reference to variables from their enclosing scope, and can cause a retain cycle, since your object has a strong reference to the block and the block has a strong reference to the object through the reference to self. By creating a weak variable weakSelf, you make the block's reference to the object that owns it weak, and avoid the retain cycle.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Got it. Its was added to a project im working on and was curious about this.Thanks! – Tony May 14 '14 at 14:43
  • "That syntax allows you to reach into **another object** and access it's instance variables directly, which is bad practice." (emphasis mine) Untrue _unless_ the ivar was specifically declared public, which is definitely not the case for a synthesized variable. – jscs May 14 '14 at 17:55
0

-> directly accesses the instance variable. weakSelf.someObject is equivalent to [weakSelf someObject].

You probably don't want to use -> for this unless you have a specific reason, like performance, or to skip KVO. Generally, it is preferred to use property access.

A side note: you should not use weakSelf for anything besides creating safeSelf. After that, you should get everything you need from safeSelf. (weakSelf can turn nil at any point if there are no longer strong references to the object.)

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0
weakSelf.someObject
safeSelf->someObject

They are both silly. Having established a strong safeSelf in the first line of your block, you should make sure it is not nil and then use it exclusively, with normal property (dot) syntax so that you pass through the accessor method and get whatever benefits it has (which might include thread safety):

safeSelf.someObject

You definitely do not want to use ->. The concern here is that your reference to self might be nil - and using -> on a nil object is a disaster.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Why check it's not `nil`? – trojanfoe May 14 '14 at 14:28
  • @trojanfoe Because the reference was weak for a while, so there is a chance that it became nil (under certain multithreading situations). – matt May 14 '14 at 14:29
  • And then using "dot syntax" will have no effect if the object is `nil`, so there is no need to check. – trojanfoe May 14 '14 at 14:30
  • @trojanfoe For example, if part of your completion block is `NSArray *array = @[safeSelf];` – Aaron Brager May 14 '14 at 14:31
  • @trojanfoe Sure, but whatever he's doing in the block might include other side effects that should not take place if `self` has vanished out from under us. I'm simply reciting the full form of the "weak-strong dance" as advocated by Apple in, say, the WWDC 2013 videos. – matt May 14 '14 at 14:31