Reputation: 45
Alright, so I found this comment:
How to resolve this error? - Class 'ViewController' has no initializers
But no one answered the question. Why does this line cause an error:
let nameOfDelegate : protocolName!
But this one doesn't:
var nameOfDelegate : protocolName!
I understand that var is a mutable property and let is a constant, but both are being initialized, correct?
Upvotes: 1
Views: 63
Reputation: 4819
There is no init() function, so none of these class members is initialized when the class is instanciated. But only one MUST be initialized when the class is instanciated: the one that is immutable, because it will not be possible to initialize it later. Therefore, add an initalizer, in an init() function or directly on the line that defines the constant.
Upvotes: 2
Reputation: 2477
In Swift optional variables are set to nil by default if you don't provide it with an initial value. Swift does not give constants default values.
Upvotes: 3
Reputation: 1292
its because its not initialized, since you will need to give it a value later, you wont be able, because its a "let", so you have to make it a var so you can later initialize it
but you could use values that are already initizalized from other places - exmaple: let appDelegate = UIApplication.shared.delegate
Upvotes: 0