Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

1458279

Suspended
Original poster
May 1, 2010
1,601
1,521
California
https://www.hackingwithswift.com/swift3

I'm watching some Swift 3 videos and blogs and noticed this new underscore option.

Seems that if you use the underscore in front of a parameter, you don't need to have a named parameter.

Code:
func sendMessage(_ message: String, to recipient: String, shouting: Bool) {
    var message = "\(message), \(recipient):"
    if shouting {
        message = message.uppercaseString
    }
    print(message)
}

sendMessage("see you at the Bash", to: "Morgan", shouting: false)

@19:30


I don't have Swift 3 yet so I can't test everything.

Q. what happens if you put the underscore under each parameter?

Does this make it so you can have no parameter names on all of the parameters or is it just the 1st parameter?
 

1458279

Suspended
Original poster
May 1, 2010
1,601
1,521
California
Watching more of the video, I see @ 23:20, this:
func filterInts(_ numbers: [int], _ includeNumber: type) -> [Int] {...}

So wouldn't that mean that the underscore can be used on any parameter?
 

eventailapp

macrumors member
Jan 7, 2016
65
14
Yes it can. In a Swift 3 playground this is legal:

Code:
func f(_ a: Int, _ b: Int) {
  print("\(a) \(b)")
}

f(1, 2)
 
  • Like
Reactions: 1458279
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.