-1

Can someone explain to me what is the purpose of self = self in this code?

// Keep the reference to the interface :
private var guidedItfRef: Ref<GuidedPilotingItf>?
private var goUp: Bool
...
guidedItfRef = provider.getPilotingItf(PilotingItfs.guided) { [weak self] guidedItf in
    if let guidedItf = guidedItf, let self = self, guidedItf.currentDirective == nil {
        self.goUp = !self.goUp
        guidedItf.moveToRelativePosition(forwardComponent: 0, rightComponent: 0,
                downwardComponent: self.goUp ? 2.0 : -3.0 , headingRotation: 0)
    }
}
Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • Recommendation: Replace `self.goUp = !self.goUp` with `self.goUp.toggle()` if using newer versions of Swift. – George Dec 25 '19 at 22:30

1 Answers1

1

When you use [weak self] inside a closure this will make self to be optional so you need to unwrap it with let

, let self = self // rhs self is optional , `self` or let strongSelf = self

like

var value:int?

unwrap option 1

guard let value = value else { return } 
// use value which now is Int not int?

unwrap option 2

if let value = value {  
   // use value which now is Int not int?
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thanks can you provide a little more detail as I'm new to swift? – Lightsout Dec 25 '19 at 22:11
  • 2
    @bakalolo I don't know if I am not understanding you correctly, but [you also said you were new to Swift](https://stackoverflow.com/q/31515805/9607863) on `Jul 20 '15` (over 4 years ago). – George Dec 25 '19 at 22:25
  • 1
    @George_E hahahahahahhahahahahaha nice notice – Shehata Gamal Dec 25 '19 at 22:26
  • @Sh_Khan I clicked on their profile after seeing their high score (that post I linked shows as the first on their profile due to its popularity). There are actually older posts they have made about Swift :p – George Dec 25 '19 at 22:27