0

I want to have a multiple optional binding for a mutable and an immutable optional strings. but this code has an error

var name: String?
let family: String?
if var temp1 = name, temp2 = family{
    print("ok")
}

error: constant family used before being initializes

  • 2
    You need to initialise `family` before using it – Hamish Mar 08 '17 at 18:39
  • 2
    ... and then `if var temp1 = name, let temp2 = family { ... }`, each optional binding gets its own `let` or `var`. – Martin R Mar 08 '17 at 18:39
  • @MartinR Thanks, but I tested, it didn't work – آژانس کتاب Mar 08 '17 at 18:42
  • It does – if `family` is initialized as pointed out in the comments and the duplicate. – Martin R Mar 08 '17 at 18:44
  • Basically an optional *constant* is nonsense. – vadian Mar 08 '17 at 19:37
  • @vadian for the most part, I agree, but [not necessarily always](https://gist.github.com/dfrib/5dcb0764f849e6d454cd57ff05109582), as constructs using "delayed initialization to the immediately following block" can sometimes be useful (in which case an optional constant _could_ have its place, if ever only instantiated in some conditional "delayed" initialization, whereafter it is only used as an immutable, that may be `nil`). – dfrib Mar 08 '17 at 21:21

0 Answers0