2015-08-14 70 views
-2

我正在用2 TableView开发一个UIViewController,遵循Swift规范我创建了一个用于管理TableViews的UITableViewDelegate和UITableViewDataSource接口的类。Swift - iOS:意外地发现零,同时展开一个可选值

class CurrencyController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
var valute = [NSManagedObject]() 
var categoria = [NSManagedObject]() 


    //.... 
} 

我写的FUNC键,支持重排的TableView:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if tableView == self.tabCurrency{ 
     // 
     let cell = tableView.dequeueReusableCellWithIdentifier("currencyRow", forIndexPath: indexPath) as! CurrencyCell 

     let thisElem = valute[indexPath.row] 

     cell.curName.text = thisElem.valueForKey("valuta") as! String? //This code generates the exception 
     let elem = thisElem.valueForKey("cambio") as! Double? //This code generates the exception 

     cell.rateCur.text = "\(elem!)" 
     return cell 
    }else{ 
     // 
     let cell = tableView.dequeueReusableCellWithIdentifier("categoryRow", forIndexPath: indexPath) as! CategoryCell 

     let thisElem = categoria[indexPath.row] 

     cell.catName.text = thisElem.valueForKey("cat_name") as! String? //This code generates the exception 
     return cell 
    } 

} 

此代码生成unexepctedly发现而展开的可选值但使用PO命令零只见该变量不包含零值...

任何想法?谢谢

UPDATE: 我写了“valute”和“categoria”对象的声明。

+1

'thisElem'是什么类型? – vadian

+0

对不起,我没有写这个埃尔姆的类型。 thisElem类型是NSManagedObject – N3tMaster

+0

编译器不会抱怨as!字符串?'。它或者是as!字符串'强制铸造或'作为?字符串可选绑定,但没有尾随问号。 – vadian

回答

0

好吧,我看到我做错了什么:我没联系我的CategoryCell和CurrencyCell类与Storyboard中的Prototype Cell一起使用。

CategoryCell和CurrencyCell是我的扩展UITableViewCell的类,用于管理TableView的所有单元。

现在我将这些类与storyboard正确链接,现在它工作正常!

0

看来:

this.valueForKey("cambio") 

应该是:

thisElem.valueForKey("cambio"). 

this - >thisElem

+0

没有thisElem是正确的,thisElem代表从链接到TableView的NSManagedObject数组中选择的元素 – N3tMaster

+0

哦,我很抱歉,我没有看到我的错误: - D ...我很惭愧..我要更新我的代码。无论如何,我解决我的问题,谢谢你的提示 – N3tMaster

相关问题