-1

I need to fill a array with values which is in a class:

class a {

     var selectedRows: [Int]?

     init() {}

     init(number: Int) {
            selectedRows?.append(number)
        }
}

How can I fill this array from another class?

adri567
  • 533
  • 5
  • 20
  • can you get that in other class? – Jawad Ali Jul 27 '20 at 08:50
  • Does this answer your question? [Trying to declare an array of custom objects in swift](https://stackoverflow.com/questions/41685593/trying-to-declare-an-array-of-custom-objects-in-swift) – Ryan Haycock Jul 28 '20 at 16:09

1 Answers1

1

you should initialitze the array. The array is not set and therefore it will not add any number.

best practice in my opininion would be:

class a {

     var selectedRows: [Int] = []

     init() {}

     init(number: Int) {
            selectedRows.append(number)
        }
}

you can also add an append function

append(number: Int) {
   selectedRows.append(number)
}

or extend the array to add custom functionality as asked here: How can I extend typed Arrays in Swift?

Lex
  • 166
  • 1
  • 5