I am new to Swift and coding so I am sorry if this is a stupid question but this error comes up when I test my code. My code is:
class Node {
var value: String
var children = [Node]()
init() {
value = " "
}
}
The error message said:
main.swift:29:21: error: argument passed to call that takes no arguments
let m0= Node(value:"wash")
And this is my instruction:
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
CodePudding user response:
You forgot to make a constructor (init function) which accept a parameter (step 7), this is why the error states the call takes no arguments. By adding the value
parameter, we can accept it and then assign it to the corresponding variable.
class Node {
var value: String
var children = [Node]()
init() {
value = ""
children = [Node]()
}
init(value: String) {
self.value = value
}
}
let m0 = Node(value: "wash")