You can somewhat think about it as array of object vs. array of references.
[SKSpriteNode]
must contain actual objects[SKSpriteNode?]
can contain either references to objects, or nil
Examples
Creating an array with 64 default SKSpriteNode
:
var sprites = [SKSpriteNode](repeatElement(SKSpriteNode(texture: nil),
count: 64))
Creating an array with 64 empty slots (a.k.a optionals):
var optionalSprites = [SKSpriteNode?](repeatElement(nil,
count: 64))
Converting an array of optionals into an array of objects (collapsing [SKSpriteNode?]
into [SKSpriteNode]
):
let flatSprites = optionalSprites.flatMap { $0 }
The count
of the resulting flatSprites
depends on the count of objects in optionalSprites
: empty optionals will be ignored, i.e. skipped.