Swift 3: Why is the _ character added before the sender in the action parameters?

My Xcode was recently upgraded to Xcode 8 with Swift 3. What I noticed is that when adding actions to the ViewController functions in this style are created:

@IBAction func methodName(_ sender: UIButton) {

}

I do not understand what the goal is to have _. Can someone explain why he is there? In previous Swift 2.2 it was not there.

Another question: the previous project, which I completed, looking at the tutorial, was transferred to Swift 3, and there was one method that looked like this:

func toggleButtons(toggle: Bool) {
    yesButton.isUserInteractionEnabled = toggle
    noButton.isUserInteractionEnabled = toggle
}

I called this method as follows: toggleButtons(false)still did not pass the name of the argument. Now migrator changed it as shown below, but nothing changed when the method was called later in the code.

func toggleButtons(_ toggle: Bool) {
    yesButton.isUserInteractionEnabled = toggle
    noButton.isUserInteractionEnabled = toggle
}

Is this a different situation from the previous case?

+4
2

( ) , _: .

, " ViewController".

, @IBAction, ( xib). , Objective-C. Objective-C, .

func methodName(sender: UIButton), Objective-C methodNameWithSender:.

func methodName(_ sender: UIButton), Objective-C methodName:.

iOS macOS -, " ", ( "" ), . ( ), . macOS, .

, , cut:, copy: paste:. , "" cut: . cut: , , ( Objective-C) cut:, cutWithSender:. cut:, , cutWithSender:, , , .

Xcode , @IBAction func action(_ sender: UIButton), , , , , .

Swift 3 , . Swift 2, , _ Swift 2 ( ). Xcode 8 (Swift 3) _, Xcode 7 (Swift 2.2) .

toggleButtons Xcode 7.

+7

, .

obj.methodName(button)

// no need for
obj.methodName(sender: button)

Swift , Swift3, . , ( _).

+1

Source: https://habr.com/ru/post/1654957/


All Articles