0

Still pretty new to Swift I want to use an array of arrays of integers. I thus write the following in the function that makes use of it.

var allSetsOfCards: [[Int]]

I don't know before the app starts running what exactly will be in it or how big it will be except that each array in it will contain three integers so I can't specify exactly what it will look like when I introduce it. Later I want to add some information to it in the following way:

allSetsOfCards[setNumber][0] = i
allSetsOfCards[setNumber][1] = j
allSetsOfCards[setNumber][2] = k

where setNumber, i, j, and k are integer-variables. This gives me the error message:

Variable 'allSetsOfCards' passed by reference before being initialized.

What do I need to do? I searched this site and found one entry that seemed to cover the topic (Two-dimensional array in Swift) but I was unable to understand it. No amount of googling seems to work either. An answer to my specific problem would be great. A brief easy to understand explanation of how swift handles arrays would be terrific.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • you can use my answer from a similar question https://stackoverflow.com/a/49735414/7435893 – Kawe May 22 '18 at 08:13

3 Answers3

4

The error message is pretty clear. What you done here is just the declaration. To use it you need initialize it either separate from declaration:

var allSetsOfCards: [[Int]]
allSetsOfCards = [[Int]]()

or along with it:

var allSetsOfCards = [[Int]]()

The next problem would raise when compiler reaches your subscript code. Since the array is pretty empty in the beginning, you'll get Index out of range error. You should either reserve capacity to be able to subscript your indexes, or append your elements to an empty array.

allSetsOfCard.append([i, j, k])
Stimorol
  • 199
  • 10
1

Although your question could be a duplicate of: Variable p passed by reference before being initialized, I would try to post a descriptive answer to your case.


What you have done so far by implementing:

var allSetsOfCards: [[Int]]

is just declaring a variable without initializing it. So what you have to do is to initialize it before using it, you would have more than one option to do this, you could simply do it like this:

var allSetsOfCards = [[Int]]()

That would fine if you are pretty sure that allSetsOfCards will be always used.

Sometimes your application won't need to work with it, for instance imagine that you'll need to fill allSetsOfCards if only specific condition(s) has/have been applied then the initialization of it would be meaningless (it is a useless wasting of memory), at this point you could declare it as lazy property:

lazy var allSetsOfCards = [[Int]]()

assuming that allSetsOfCards is a property of a struct or a class, at this point it won't be initialized unless you need to use it.


Aside note:

Also, you have to make sure that the inner arrays are also already initialized, example:

var allSetsOfCards = [[Int]]()

// that's what I mean:
var firstSet = [Int]()

allSetsOfCards.append(firstSet)

// assuming that `setNumber` is 0 so far...
var setNumber = 0

allSetsOfCards[setNumber].append(i)
allSetsOfCards[setNumber].append(j)
allSetsOfCards[setNumber].append(k)

// OR (as shorter option):
allSetsOfCards[setNumber].append([i, j, k])

Do you know Swift does has Sets? You might want to let allSetsOfCards to be an array of sets ([Set<Int>]()) instead .

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
-1

Try this...

you can initialize array of array like this

var intArrayOfArray: [[Int]] = [[Int]]()

below line will throw an error because at this point the array is only initialised and does not contain any element, hence Array index out of bound error.

 //print(intArrayOfArray[0]) //output:: error indexOutOfBound

intArrayOfArray.append([1,2,3]) 

print(intArrayOfArray[0]) // output:: [1,2,3]

intArrayOfArray.append([4,5])

print(intArrayOfArray[1]) // [4,5]

intArrayOfArray[0] = [6, 7]

print(intArrayOfArray[0]) // [6, 7]
Shilpriya
  • 93
  • 6
DEEPAK KUMAR
  • 341
  • 4
  • 8