代码之家  ›  专栏  ›  技术社区  ›  pguardiario

swift错误:调用中的额外参数

  •  0
  • pguardiario  · 技术社区  · 7 年前

    当函数的参数与传递的参数相同时,为什么我会得到这个错误?

    class ViewController: NSViewController {
    
       func foo(path: String, arguments: [String], showOutput: Bool) -> Void {
       }
    
        @IBAction func a1(_ sender: NSButton) {
            let path = "/sbin/ping"
            let arguments = ["-c", "5", "google.com"]
            self.foo(path, arguments, true){ // I'm getting extra argument in call for true
        }
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   iOS Geek    7 年前
    func foo(_ path: String,_ arguments: [String],_ showOutput: Bool) -> Void {
        /// Prerform task here whic you want to perform while calling this
        /// function or task with those Paramaters
    }
    
    @IBAction func a1(_ sender: NSButton) {
        let path = "/sbin/ping"
        let arguments = ["-c", "5", "google.com"]
    
        /// It is just a normal Function that accepts parameteres and
        /// Preform requred Task
        self.foo(path, arguments, true)
    }
    
        2
  •  0
  •   rmaddy    7 年前

    线(带着神秘的额外 { 最后):

    self.foo(path, arguments, true){ 
    

    需要:

    self.foo(path: path, arguments: arguments, showOutput: true)
    

    使用 self. 是可选的。