Swift - 未设置默认属性值

时间:2022-09-25 12:36:19

Why properties are not initialized with default values? If I uncomment

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

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

Everything works fine.

一切正常。

Swift  - 未设置默认属性值

UPDATE:

更新:

Looks like the problem related to generic type "Element", the following code works as expected:

看起来与泛型类型“元素”相关的问题,以下代码按预期工作:

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
    }
}

2 个解决方案

#1


0  

First declear like this later you just add (or) initilize the data

首先如此删除你只是添加(或)初始化数据

 var sections: [[Element]]?

#2


0  

// simplified example of your trouble

class C<T> {
    var arr: [T] = []
    func f() {

        // (1) it seems that the type of the container is exactly the same,
        //     as the type of the arr defined in step (3)

        print(arr.dynamicType)  // Array<Int>
        print(T.self)           // Int
        /*
        so far, so good ..
        can our arr collect integer numbers ????
        */

        //arr.append(1)       // error: cannot convert value of type '[Array<Int>]' to expected argument type '[_]'

        print(arr)              // []

        // why?
        // see the propper use of generic class at (4)
    }
}

let c = C<Int>()                // C<Int>
c.f()

// (2)
print(Int.self)                 // Int
var arr = [Int]()               // []

// (3)
print(arr.dynamicType)          // Array<Int>
arr.append(2)                   // [2]

// (4)
c.arr.append(1)
c.arr.append(2)
c.f()                           // [1, 2]
print(c)                        // C<Swift.Int>

let d = C<String>()
//c.arr.append("alfa")            // error: Cannot convert value of type 'String' to expected argument type 'Int'
d.arr.append("alfa")
print(d)                        // C<Swift.String>

#1


0  

First declear like this later you just add (or) initilize the data

首先如此删除你只是添加(或)初始化数据

 var sections: [[Element]]?

#2


0  

// simplified example of your trouble

class C<T> {
    var arr: [T] = []
    func f() {

        // (1) it seems that the type of the container is exactly the same,
        //     as the type of the arr defined in step (3)

        print(arr.dynamicType)  // Array<Int>
        print(T.self)           // Int
        /*
        so far, so good ..
        can our arr collect integer numbers ????
        */

        //arr.append(1)       // error: cannot convert value of type '[Array<Int>]' to expected argument type '[_]'

        print(arr)              // []

        // why?
        // see the propper use of generic class at (4)
    }
}

let c = C<Int>()                // C<Int>
c.f()

// (2)
print(Int.self)                 // Int
var arr = [Int]()               // []

// (3)
print(arr.dynamicType)          // Array<Int>
arr.append(2)                   // [2]

// (4)
c.arr.append(1)
c.arr.append(2)
c.f()                           // [1, 2]
print(c)                        // C<Swift.Int>

let d = C<String>()
//c.arr.append("alfa")            // error: Cannot convert value of type 'String' to expected argument type 'Int'
d.arr.append("alfa")
print(d)                        // C<Swift.String>