Is it safe to force unwrap variables that have been optionally accessed in the same line of code?

后端 未结 5 943
无人及你
无人及你 2020-12-14 05:52
someFunction(completion: { [weak self] in
    self?.variable = self!.otherVariable
})

Is this always safe? I access the optional <

5条回答
  •  情歌与酒
    2020-12-14 06:34

    The documentation clearly states that, if the left side of the assignment is determined to be nil, right side will not be evaluated. However, in the given example self is weak reference and may be released (and nullified) right after the optional check passes, but just before force-unwrap will happen, making the whole expression nil-unsafe.

提交回复
热议问题