A single expression with the same effect as your code is
funcThatReturnsOptional().map { object.nonOptionalProperty = $0 }
but your code is definitely better readable.
Here the map()
method of Optional
is used and the closure is
executed only if the function does not return nil
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…