I know a guard statement can be used like this
guard let someConstant = someOptional() else {
// ...
}
And I tried to do
struct MyStruct {
let aString: String
init?() {
guard aString = optionalString() else { return }
}
// ...
but it doesn't seem to work.
I assumed that the let a = b and a = b would somehow have a boolean value that was false when it failed, but are guard let and guard actually completely different?
Optional binding in
ifstatement works like that: it checks if given value has value and if it has it goes with this value toifblockWith optional binding in
guardstatement it checks if value exists and if does, it continue in current scope with variable / constant assigned in this scopeSo, for your initializer you need to assign constant, so you need to use
letkeyword and since your initalizer is optional, you need to returnnilif initalization fails