我是 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
uj5u.com熱心網友回復:
您忘記創建一個接受引數的建構式(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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344348.html
