Aloogy Aloogy - 1 year ago 71
iOS Question

Swift3: Using Two Arrays to populate grouped Table

I currently have two populated arrays using a custom struct.

struct Group {
var id: String
var type: String
var desc: String
var name: String

init() {
id = ""
type = ""
desc = ""
name = ""

Data gets appended to:

var clientArray: [Group] = []
var departmentArray: [Group] = []

I essentially want to join them together to have the format something like [[clientArray], [departmentArray]] so I can use "section" and populate two different groups on a table with the respective arrays.

So far I've tried the following, but I get the error "fatal error: index out of range".

var masterArray = [[Group]]()
//Then further down the page...
self.masterArray[0] = self.clientArray
self.masterArray[1] = self.departmentArray

How can I get this to work? Thanks for any help.

Answer Source

You could write:

var masterArray = [self.clientArray, self.departmentArray]

Otherwise use append:. The docs state:

You can’t use subscript syntax to append a new item to the end of an array.