![](/img/trans.png)
[英]Cannot invoke append with an argument list of type json in IOS Swift
[英]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
?
首先解決sections [0]問題:嘗試訪問它時沒有section [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 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.