Reputation: 791
I'm new to Swift and I want to be able to write a property setter, which will also be used as a constructor when initializing:
struct Person {
private var name: String {
get {
return self.name;
}
set {
self.name = name;
}
}
}
var Murad = Person(name: "Murad");
When I run this code error return this error
argument passed to call that takes no arguments
Upvotes: 0
Views: 469
Reputation: 285039
The error occurs because the property is a computed property and it's private (can only be changed within the class), so for the compiler there is no (memberwise) initializer and only the default initializer Person()
without parameter can be used.
You are in luck that you get this error message otherwise when you would run the code you will run into an infinite loop (which causes an overflow crash).
If you want a constant just declare the struct member as let
struct Person {
let name: String
}
let murad = Person(name: "Murad")
print(murad)
Upvotes: 2