【问题标题】:Swift Error: Extra argument in callSwift 错误:调用中的额外参数
【发布时间】:2018-06-21 12:15:56
【问题描述】:

当函数的参数与传递的参数相同时,为什么会出现此错误?

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
    }
}

【问题讨论】:

    标签: swift


    【解决方案1】:
    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)
    }
    

    【讨论】:

    • 这行得通,谢谢。我得试着记住那些奇怪的_的
    • 欢迎您,乐于助人
    • @pguardiario 实际上并不需要“奇怪的”_。它们是可选的。
    【解决方案2】:

    一行(末尾有神秘的额外{):

    self.foo(path, arguments, true){ 
    

    需要:

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

    self. 的使用是可选的。

    【讨论】:

      猜你喜欢
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多