I want to try to write a default init
in a protocol
extension, so I did this:
protocol P {
var data: [AnyHashable: Any] { get }
init(_ s: String)
}
extension P {
init(_ s: String) {
self.data = ["s": s]
}
}
But I'm getting this error:
'let' property 'data' may not be initialized directly; use "self.init(...)" or "self = ..." instead
I have no idea what it means. Thanks for your help!
The error says that the variable cannot be initialized in the protocol extension because it's declared as constant { get }
But even if you declare the variable { get set }
you can't use the init
method anyway without a constraint to a concrete type or an associated type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With