ios - Swift - 未设置默认属性值

标签 ios objective-c iphone swift swift2

为什么属性没有用默认值初始化?如果我取消注释

//    required init?(coder aDecoder: NSCoder)
//    {
//        super.init(coder: aDecoder)
//    }

一切正常。

enter image description here

更新:

看起来问题与泛型类型“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/

相关文章:

ios - iPhone中的API请求认证

ios - Xcode 10.1 归档时不显示 'distribute app' 选项,无法归档

iphone - 使用 GZIP 或 deflate 在 objective-c (iphone) 中压缩/解压缩 NSString

iphone - 我正在重新制作一个小应用程序,以更好地在iOS上使用MVC模式。

ios - 检测 UITableView HeaderView 中 UISegmentedControl 的变化

ios - Swift:无法在 TableViewController ( Storyboard)上创建按钮操作

ios - 界面生成器中的自动调整大小问题

ios - 匹配当前日期到对象日期的位置通知

ios - 由于按钮栏,横向 100% 的体高溢出

ios - UIButton 子类 @IBAction 不起作用