简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM