Looking at the following code (playground ready).
I'm unable to append data to an unwrapped optional array from within an if let
statement.
What's the best method to allow appending for an optional array?
class ValueTypeTest {
var data: [Int]?
func tryAppend() {
if var unwrappedData = self.data {
unwrappedData += [2, 3] // problem !
self.data! += [4, 5] // works
} else {
// Initialize paging and initial results
self.data = [0, 1]
}
}
}
var v = ValueTypeTest()
v.data // nil
v.tryAppend()
v.data // [0, 1]
v.tryAppend()
v.data // expected: [0, 1, 2, 3, 4, 5] actual: [0, 1, 4, 5]