Does "_" in Swift 3 work on 1st only or all params?

Discussion in 'iOS Programming' started by 1458279, Sep 25, 2016.

  1. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #1
    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?
     
  2. 1458279 thread starter Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    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?
     
  3. eventailapp macrumors member

    eventailapp

    Joined:
    Jan 7, 2016
    #3
    Yes it can. In a Swift 3 playground this is legal:

    Code:
    func f(_ a: Int, _ b: Int) {
      print("\(a) \(b)")
    }
    
    f(1, 2)
    
     
  4. 1458279 thread starter Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #4

Share This Page