我是新的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
您忘记创建一个接受参数的构造函数(init函数)(步骤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")