How to create an array of fixed size objects

In Swift, I am trying to create an array of 64 SKSpriteNode. I want to initialize it empty first, then I would put Sprites in the first 16 cells and the last 16 cells (simulating a chess game).

From what I understood in the document, I would expect something like:

var sprites = SKSpriteNode()[64];

or

var sprites4 : SKSpriteNode[64];

But that will not work. In the second case, I get an error: "Arrays of fixed length are not yet supported." Could this be real? For me, this sounds like a major feature. I need to access an element directly by their index.

+57
ios8 swift xcode6
Jun 24 '14 at 19:51
source share
5 answers

Fixed-length arrays are not yet supported. What does it mean? Not that you cannot create an array of n many things - obviously, you can just do let a = [ 1, 2, 3 ] to get an array of three Int s. This means that the size of the array is not what you can declare as type information.

If you need an array from nil s, you will first need an array of an optional type - [SKSpriteNode?] , And not [SKSpriteNode] - if you declare a variable of an optional type, be it an array or a single value, it cannot be nil . (Also note that [SKSpriteNode?] different from [SKSpriteNode]? ... you need an optional array, not an optional array.)

Swift is very clear in design, requiring variable initialization, since assumptions about the contents of uninitialized links are one way in which C programs (and some other languages) become errors. So, you need to explicitly request the [SKSpriteNode?] containing 64 nil s:

 var sprites = [SKSpriteNode?](count:64, repeatedValue: nil) 

[SKSpriteNode?]? this actually return [SKSpriteNode?]? though: an extra array of extra sprites. (A bit strange, since init(count:,repeatedValue:) will not be able to return zero.) To work with an array, you need to expand it. There are several ways to do this, but in this case, I would prefer the optional binding syntax:

 if var sprites = [SKSpriteNode?](count:64, repeatedValue: nil) { sprites[0] = pawnSprite } 
+85
Jun 24 '14 at 20:20
source share

The best thing you can do now is create an array with an initial counter repeating zero:

 var sprites = [SKSpriteNode?](count: 64, repeatedValue: nil) 

Then you can fill in any values ​​you want.




In Swift 3.0 :

 var sprites = [SKSpriteNode?](repeating: nil, count: 64) 
+43
Jun 24 '14 at 20:12
source share

Declare an empty SKSpriteNode, so there will be no need to refuse

 var sprites = [SKSpriteNode](count: 64, repeatedValue: SKSpriteNode()) 
+3
Aug 17 '16 at 1:34
source share

Currently, the semantically closest is a tuple with a fixed number of elements.

 typealias buffer = ( SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode, SKSpriteNode) 

But this (1) is very inconvenient to use and (2) the memory layout is undefined. (at least not known to me)

+2
May 03 '17 at 10:34 a.m.
source share

One thing you could do is create a dictionary. It might be a little messy, considering you are looking for 64 elements, but it does the job. I'm not sure if this is the "preferred way", but it worked for me using an array of structures.

 var tasks = [0:[forTasks](),1:[forTasks](),2:[forTasks](),3:[forTasks](),4:[forTasks](),5:[forTasks](),6:[forTasks]()] 
-2
Apr 7 '16 at 16:16
source share



All Articles