Multidimensional arrays From C to Swift -



Multidimensional arrays From C to Swift -

in c have next multidimensional array:

unsigned wins[8][3] = {{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};

to access elements utilize next code:

int i; for(i = 0; < 8; ++i) { unsigned *positions; positions = wins[i]; unsigned pos0 = positions[0]; unsigned pos1 = positions[1]; unsigned pos2 = positions[2]; if(arraypassedin[pos0] != 0 && arraypassedin[pos0] == arraypassedin[pos1] && arraypassedin[pos0] == arraypassedin[pos2]) { // here }

i know in swift can like:

var array = array<array<int>>()

but i'm not sure if produces same result accessing elements.

you can create multi-dimentsional array in pretty similar manner c code:

var wins = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]

the swift code utilize same way c code pretty similar; main difference using for-in loop instead of standard for loop (although too).

for positions in wins { var pos0 = positions[0] var pos1 = positions[1] var pos2 = positions[2] if(arraypassedin[pos0] != 0 && arraypassedin[pos0] == arraypassedin[pos1] && arraypassedin[pos0] == arraypassedin[pos2]) { // here } }

do note that, though there similarities, arrays in swift not arrays in c. instance, when you're looping on wins, you're creating copies of positions arrays (the actual memory re-create happens if write array, there's not performance penalty). if set, say, positions[0] different value, value not updated in wins if c.

multidimensional-array swift

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -