Reputation: 39243
Here is the code I have:
struct Algorithm {
let category: String = ""
let title: String = ""
let fileName: String = ""
}
let a = Algorithm(
The autocomplete shows the only valid initializer:
But I was expecting to use the implicit initializer like
Algorithm(category: "", title: "", fileName: "")
This gives the error:
Argument passed to call that takes no arguments
There are even screenshots on another issue that shows this call being successfully used.
What am I doing wrong?
Upvotes: 2
Views: 1785
Reputation: 534895
The problem is the let
. If you declare your properties with var
, you'll get the memberwise initializer:
struct Algorithm {
var category: String = ""
var title: String = ""
var fileName: String = ""
}
let alg = Algorithm(category: "", title: "", fileName: "")
But since you supplied default values for all the properties, you don't get the memberwise initializer for let
properties. The rule is that you get the implicit memberwise initializer for stored properties without a default value and for var
properties (provided you have no explicit declared initializer).
Upvotes: 6
Reputation: 17534
use
struct Algorithm {
var category: String = ""
var title: String = ""
var fileName: String = ""
}
and autocomplete will show you both possibilities
Upvotes: 1
Reputation: 25459
You provided the default values for the properties that's why compiler won't add the default initialiser. When you remove the default values you will get what you are expecting:
struct Algorithm {
let category: String
let title: String
let fileName: String
}
Upvotes: 5