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

标签 ios swift ios8

两个类:

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

最佳答案

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

关于ios - Swift:无法使用类型的参数列表调用 'append',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32109100/

相关文章:

ios - 添加导航栏时缩小 Collection View

swift - 我如何重写此 switch 语句以便以特定方式对字符串数组进行排序

ios - 单元测试未在 IOS swift 5 上运行

ios - Swift 1.2 和解析 : Issue with retrieving images to populate PFQueryCollectionViewController

iOS 8 在没有搜索栏的情况下显示搜索结果

ios - UIScreen.main 、 super.view 和其他 View 的中心不在屏幕中间居中

c++ - 如何访问嵌套的plist目录cocos2dx?

ios - 可在 IOS 浏览器中下载应用程序

ios - 为什么模拟器和我的 iPhone 之间的颜色不同?

ios - iOS8 中的扩展 - 由于没有系统应用程序而终止