Adding elements to optional arrays in Swift

You can use the fact that methods called via optional chaining always return an optional value, which is nil if it was not possible to
call the method:

if (myArray?.append(99)) == nil {
    myArray = [99] 
}

If myArray != nil then myArray?.append(99) appends the new element
and returns Void, so that the if-block is not executed.

If myArray == nil then myArray?.append(99) does nothing and returns
nil, so that the if-block is executed and assigns an array value.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)