In Swift, I am trying to create an array of 64 SKSpriteNode. I want first to initialize it empty, then I would put Sprites in the first 16 cells, and the last 16 cells (simulating an chess game).
From what I understood in the doc, I would have expect something like:
var sprites = SKSpriteNode()[64];
or
var sprites4 : SKSpriteNode[64];
But it doesn't work. In the second case, I get an error saying: "Fixed-length arrays are not yet supported". Can that be real? To me that sounds like a basic feature. I need to access the element directly by their index.
Declare an empty SKSpriteNode, so there won't be needing for unwraping
Fixed-length arrays are not yet supported. What does that actually mean? Not that you can't create an array of
n
many things — obviously you can just dolet a = [ 1, 2, 3 ]
to get an array of threeInt
s. It means simply that array size is not something that you can declare as type information.If you want an array of
nil
s, you'll first need an array of an optional type —[SKSpriteNode?]
, not[SKSpriteNode]
— if you declare a variable of non-optional type, whether it's an array or a single value, it cannot benil
. (Also note that[SKSpriteNode?]
is different from[SKSpriteNode]?
... you want an array of optionals, not an optional array.)Swift is very explicit by design about requiring that variables be initialized, because assumptions about the content of uninitialized references are one of the ways that programs in C (and some other languages) can become buggy. So, you need to explicitly ask for an
[SKSpriteNode?]
array that contains 64nil
s:This actually returns a
[SKSpriteNode?]?
, though: an optional array of optional sprites. (A bit odd, sinceinit(count:,repeatedValue:)
shouldn't be able to return nil.) To work with the array, you'll need to unwrap it. There's a few ways to do that, but in this case I'd favor optional binding syntax:One thing you could do would be to create a dictionary. Might be a little sloppy considering your looking for 64 elements but it gets the job done. Im not sure if its the "preferred way" to do it but it worked for me using an array of structs.
Swift 4
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, ornil
Examples
Creating an array with 64 default
SKSpriteNode
:Creating an array with 64 empty slots (a.k.a optionals):
Converting an array of optionals into an array of objects (collapsing
[SKSpriteNode?]
into[SKSpriteNode]
):The
count
of the resultingflatSprites
depends on the count of objects inoptionalSprites
: empty optionals will be ignored, i.e. skipped.For now, semantically closest one would be a tuple with fixed number of elements.
But this is (1) very uncomfortable to use and (2) memory layout is undefined. (at least unknown to me)
The best you are going to be able to do for now is create an array with an initial count repeating nil:
You can then fill in whatever values you want.
In Swift 3.0 :