I have a question regarding Swift and additional properties.
Suppose I have the following code:
import Foundation
class MyClass: NSObject {
var parent: MyClass?
var string: String?
init() {
super.init()
}
}
let variable : MyClass = MyClass()
variable.string = "variable"
variable.parent?.string = "parent"
I get an error in the following line: "Unable to assign the result of this expression"
variable.parent?.string = "parent"
Now I can suppress this error by replacing the question mark with an exclamation mark, which, as far as I know, will force Swift to assume that the object will be at run time, but it will work because, obviously, there is no object.
Essentially, how can I set a property on an optional variable without doing something like "if variable.parent"?
source
share