Linked Questions

Popular Questions

How to concatenate an optional array to another array in Swift?

Asked by At

I know this sounds ridiculous but I am seriously confused now. Basically I have two vars, one is [Data] and one is Array<Data>?

I need to combine these two. I've tried do var1+var2, which is giving me errors say can't do binary operations on these two. So I googled, I can use append method, now there comes more questions:

  1. the append method is crossed out in autofill, so I am not sure if I should use it.
  2. Even if I try to use it (while it's being crossed out), it is still giving me errors

Here is the code and errors I get:

var a1:[Data] //a return value from other function
var a2:Array<Data>? //a parameter that's passed in

a1.append(contentsOf:a2) //Cannot use mutating member on immutable value: function call returns immutable value
a1+a2 //Binary operator '+' cannot be applied to operands of type 'Array<Data>' and 'Array<Data>?'

Both arrays can be empty, how can I concatenate these two arrays?

Related Questions