【问题标题】:argument passed to call that takes no argument传递给不带参数的调用的参数
【发布时间】:2021-10-30 21:45:04
【问题描述】:

我是 Swift 和编码的新手,所以如果这是一个愚蠢的问题,我很抱歉,但是当我测试我的代码时会出现这个错误。 我的代码是:

class Node {
  var value: String
  var children = [Node]()
  init() {   
  value = " "
  }
}

错误信息说:

main.swift:29:21: error: argument passed to call that takes no arguments
let m0= Node(value:"wash")

这是我的指示:

1. Edit a file named "main.swift"                                                  
2. Create a class called Node                                                   
3. Do not specify access modifiers
4. Create a property called "value" of type string                                
5. Create a property called "children" of type array of Nodes
6. Create a default constructor which initializes value to an empty string and children to an empty array
7. Create a constructor which accepts a parameter named value and assigns it to the appropriate property 

【问题讨论】:

  • 提示:您还没有完成第 7 步。另外,请确保在 let m0 行中的 = 之前有一个空格。

标签: arrays swift tree


【解决方案1】:

您忘记创建一个接受参数的构造函数(初始化函数)(第 7 步),这就是错误指出调用不接受参数的原因。通过添加value参数,我们可以接受它,然后赋值给对应的变量。

class Node {
  var value: String
  var children = [Node]()

  init() {
      value = ""
      children = [Node]()
  }

  init(value: String) {   
    self.value = value
  }
}

let m0 = Node(value: "wash")

【讨论】:

  • OP 的指令列表暗示应该有两个不同的构造函数。
  • 很好,我已经编辑了我的答案。
猜你喜欢
  • 1970-01-01
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多