无法在DataController Init中的属性初始化程序错误中使用实例成员

时间:2018-03-03 20:02:45

标签: ios swift xcode swift4

我有一个应该接收数据的数据控制器,然后把它放到UITableViewDataSource中。我似乎无法访问init中的变量。

如果我取消注释init中的var sections声明,那么我得到一个错误“DataController1类型的值没有成员节”

有人可以告诉我我错过了什么吗?

相关的代码行如下:

class DataController1: NSObject, DataController {

    struct Section {
        let type: SectionTypeEnum
        let rows: [Any]
    }

    let contactData: ContactData
    var section1: [Section1]!
    var section2: [Section2]!
    var section3: [Section3]!
//    var sections = [Section]!


    init(contactData: ContactData) {
        self.contactData = contactData

        self.section1 = self.contactData.list1
        self.section2 = self.contactData.list2
        self.section3 = self.contactData.list3

//        var sections: [Section] = [
//            Section(type: .section1, rows: section1),
//            Section(type: .section2, rows: section2),
//            Section(type: .section3, rows: section3),
//]

    }



    var sections: [Section] = [

        Section(type: .section1, rows: section1),
//Cannot use instance member 'section1' within property initializer; property initializers run before 'self' is available
// current error
        Section(type: .section2, rows: section2),
        Section(type: .section3, rows: section3),
    ]
}

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的那样,出现错误是因为您不能声明具有相互依赖的初始值的属性

我建议仅支持datetime price 2017-10-02 08:05:00 12877.0 2017-10-02 08:06:00 12875.5 2017-10-02 08:07:00 12875.5 2017-10-02 08:08:00 12875.5 2017-10-02 08:09:00 12875.5 2017-10-02 08:10:00 12878.0 2017-10-02 08:11:00 12878.0 2017-10-02 08:12:00 12878.0 2017-10-02 08:13:00 12881.0 2017-10-02 08:14:00 12882.0 2017-10-02 08:15:00 12880.5 2017-10-02 08:16:00 12880.5 2017-10-02 08:17:00 12879.0 2017-10-02 08:18:00 12879.0 2017-10-02 08:19:00 12879.0 2017-10-02 08:20:00 12878.5 方法。

永远不要将属性声明为隐式展开的可选项,它们在init(contactData方法中使用非可选值进行初始化。如果它们应该是可选的,则将它们声明为常规可选(init

?