Reputation: 10764
Two Classes:
import UIKit
struct ListSection {
var rows : [ListRow]?
var sectionTitle : String?
}
import UIKit
struct ListRow {
var someString: String?
}
Now when I try to append:
var row = ListRow()
row.someString = "Hello"
var sections = [ListSection]()
sections[0].rows.append(row) // I get the following error:
//Cannot invoke 'append' with an argument list of type (ListRow)'
If I try this:
sections[0].rows?.append(row) // I get the following error:
//Will never be executed
How do I append to rows
in section[0]
?
Upvotes: 1
Views: 2289
Reputation: 70110
You need to at least have one ListSection
in your sections
array, but you also need the rows
array in each ListSection
to be initialized or empty instead of a nil Optional.
struct ListRow {
var someString: String?
}
struct ListSection {
var rows = [ListRow]()
var sectionTitle : String?
}
var row = ListRow()
row.someString = "Hello"
var sections = [ListSection]()
sections.append(ListSection())
sections[0].rows.append(row)
Upvotes: 1
Reputation: 24714
You need to add a ListSection
to the sections array first
var sections = [ListSection]()
var firstSection = ListSection(rows:[ListRow](), sectionTitle:"title")
sections.append(firstSection)
var row = ListRow()
row.someString = "Hello"
sections[0].rows!.append(row)
Upvotes: 1
Reputation: 11555
Start with fixing the sections[0] problem: There is no sections[0] at the time you attempt to access it. You need to append at least one section before accessing sections[0].
Upvotes: 1