【问题标题】:Swift - Default Property Values are not setSwift - 未设置默认属性值
【发布时间】:2015-11-04 08:13:01
【问题描述】:

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

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

一切正常。

更新

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

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

【问题讨论】:

  • 如果您发布代码,而不是粘贴图片,您将吸引更好的受众和更快的解决方案。另外,请详细说明您面临的问题。
  • 如果您怀疑调试器的变量窗格显示的内容,然后打印值(通过调试控制台中的命令或右键单击该值并选择打印)。有时值不同。如果是这种情况,打印它们就说明了真相。只是不要问我为什么。
  • 如果你从你的代码中删除 debugPrint(sections),它可以工作吗?
  • 如预期的那样......顺便说一句,debugPrint 中的崩溃与 v2.1.1 中解决的 swift bug 有关

标签: ios objective-c iphone swift swift2


【解决方案1】:

首先像这样删除,稍后您只需添加(或)初始化数据

 var sections: [[Element]]?

【讨论】:

    【解决方案2】:
    // 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 2010-10-16
      • 2012-03-07
      • 2012-04-07
      • 2011-09-16
      • 2011-03-21
      相关资源
      最近更新 更多