0

I found the following code in my colleague's code.
What does this mean?
headerView and operateBar are subclasses of UIView.
IMPORTANNT: The following code is not part of the if statement.
And the headerView and operateBar are not lazy properties that need to be initialized.

_ = self.headerView
_ = self.operateBar
Steper
  • 17
  • 4
  • alternative for check against `null` value – Ratul Sharker Dec 11 '18 at 09:14
  • @RatulSharker, no it isn't – user28434'mstep Dec 11 '18 at 09:22
  • please explain @user28434 – Ratul Sharker Dec 11 '18 at 09:23
  • so you are complaining about `nil` & `null`. @user28434 – Ratul Sharker Dec 11 '18 at 09:26
  • Mostly i see this portion of code within a `if` statement. Only `_ = self.headerView` has no meaning in it. – Ratul Sharker Dec 11 '18 at 09:29
  • Good to know another one single usage of this syntax. But in that case `self.headerView` would suffice, right ? – Ratul Sharker Dec 11 '18 at 09:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185051/discussion-between-user28434-and-ratul-sharker). – user28434'mstep Dec 11 '18 at 09:37
  • I distinctly remember in one of the Stanford courses, where the lecturer mentions that _ basically, means that you're telling Swift that you are aware you have not names the variable appropriately but are obligated to return sometime in future and rename it properly. Your colleague probably had a proper name there. But upon seeing that the variable was not used further for any manipulation/calculation, Swift might have encouraged him to change it to _ which he did and forgot about. Just my two cents. – iOSer Dec 11 '18 at 10:57

1 Answers1

0

If you see it as a part of if statement it checks if headerView or operateBar is not nil. So this:

if let _ = self.headerView {
    //some code
}

is equivalent of:

if self.headerView != nil {
    //some code
}
Maciej Gad
  • 1,701
  • 16
  • 21
  • @user28434 good point forgot about `let`. I've updated the answer. – Maciej Gad Dec 11 '18 at 09:28
  • 1
    And still, it's `if let` performing `nil` check, `_` here is not related to any checks, it just shows that you don't care about value of `self.headerView`. And latter here is more preferable. – user28434'mstep Dec 11 '18 at 09:29