2015-11-04 164 views
0

为什么属性未使用默认值进行初始化?如果我取消注释Swift - 未设置默认属性值

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

一切工作正常。

enter image description here

UPDATE

看起来与普通型 “元” 的问题,下面的代码按预期工作:

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

而不是粘贴的图片,如果你发布的代码,你会更好地吸引观众和更快的解决方案。另外,请详细说明您面临的问题和问题。 – Abhinav

+0

如果您怀疑调试器的变量窗格显示了什么,然后打印值(通过调试控制台中的命令或右键单击该值并选择打印)。有时候这些值是不同的。当达到这种情况时,印刷他们说实话。只是不要问我为什么。 –

+0

如果您从您的代码中删除debugPrint(部分),它可以工作吗? – user3441734

回答

0

首先declear这样以后您只需添加(或)初始化数据

var sections: [[Element]]? 
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>