In my Ball.swift
file, I declared the following variable:
let r:Double = 0
Now I have the following initializer:
init(x:Double, y:Double, radius:Double) {
self.r = radius
}
I am getting the following error:
Cannot assign to 'r' in 'self'
However, in Apple's Swift docs here, they say:
You can assign a value to a constant property at any point during initialization, as long as it is set to a definite value by the time initialization finishes.
I don't understand. Shouldn't this work? I have searched a lot online but can't find anything. I was thinking about just making it a variable, but I feel like that would be giving up. :)
You need to declare
r
with no initial value, like so:As of Swift 1.2, a constant can only be assigned once. If you give it a default value you cannot give it another value in
init
.