两个课程:
import UIKit
struct ListSection {
var rows : [ListRow]?
var sectionTitle : String?
}
import UIKit
struct ListRow {
var someString: String?
}
现在我试图追加:
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)'
如果我试试这个:
sections[0].rows?.append(row) // I get the following error:
//Will never be executed
如何追加rows
中的section[0]
?
答案 0 :(得分:1)
从修复部分[0]开始问题:在您尝试访问部分时没有部分[0]。在访问[0]部分之前,您需要至少附加一个部分。
答案 1 :(得分:1)
您需要先将ListSection
添加到sections数组
var sections = [ListSection]()
var firstSection = ListSection(rows:[ListRow](), sectionTitle:"title")
sections.append(firstSection)
var row = ListRow()
row.someString = "Hello"
sections[0].rows!.append(row)
答案 2 :(得分:1)
您的ListSection
数组中至少需要一个sections
,但您还需要在每个rows
中初始化ListSection
数组,而不是nil可选。
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)