我正在用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”对象的声明。
'thisElem'是什么类型? – vadian
对不起,我没有写这个埃尔姆的类型。 thisElem类型是NSManagedObject – N3tMaster
编译器不会抱怨as!字符串?'。它或者是as!字符串'强制铸造或'作为?字符串可选绑定,但没有尾随问号。 – vadian