为什么属性没有用默认值初始化?如果我取消注释
// required init?(coder aDecoder: NSCoder)
// {
// super.init(coder: aDecoder)
// }
一切正常。
更新:
看起来问题与泛型类型“Element”有关,以下代码按预期工作:
import UIKit
class ANTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
@IBOutlet weak var tableView: UITableView!
var sections: [[SPMenuItem]] = [[SPMenuItem]]()
var a = "qwe"
var configureCellBlock : ((UITableView, NSIndexPath, SPMenuItem) -> UITableViewCell)!
var didSelectElementBlock : ((SPMenuItem) -> Void) = { (element) -> Void in }
func elementForIndexPath(indexPath: NSIndexPath) -> SPMenuItem
{
let elements = sections[indexPath.section]
let element = elements[indexPath.row]
return element
}
// required init?(coder aDecoder: NSCoder)
// {
// super.init(coder: aDecoder)
// }
// MARK: - UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int { return sections.count }
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sections[section].count }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let element = elementForIndexPath(indexPath)
assert(configureCellBlock != nil, "configureCellBlock should not be nil!")
let cell = configureCellBlock(tableView, indexPath, element)
return cell
}
}
最佳答案
First declear like this later you just add (or) initilize the data
var sections: [[Element]]?
关于ios - Swift - 未设置默认属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33516770/