I have an array of object :
var fooArray = [Foo]()
If I want to append another array with Foo object i can use the +=
:
fooArray += anotherFooArray
This one works.
But if i'm making the Foo objects in the array optional :
var fooArray = [Foo?]()
Doing the concatenation raise an error :
[Foo?]() is not identical to 'CGFloat'
I definitely don't understand what's the problem and what CGFloat type has to do with this ?
The problem is that
Foo
andFoo?
are 2 different types (an optional is actually an instance of theOptional<T>
enum).In order to append into an array, the elements must be of the same type -
Foo
andOptional<Foo>
aren't.You can fix the problem by simply casting the array to append to an array of optional
Foo
s:As for the misleading error message, I think that it's trying to apply an overload of the
+=
operator taking a CGFloat as one of the arguments, because it can't match the provided parameters with the overload defined for the array type.