Swift: 无法使用类型的参数列表调用 'append'

Swift: Cannot invoke 'append' with an argument list of type

两个类:

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

如何在 section[0] 中附加到 rows

从修复部分[0] 问题开始:在您尝试访问时没有部分[0]。在访问 sections[0].

之前,您需要至少追加一节

您需要先添加一个 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)

您的 sections 数组中至少需要一个 ListSection,但您还需要每个 ListSection 中的 rows 数组进行初始化或清空一个 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)