Swift 如何在运行时更改变量类型
我有一个Swift 如何在运行时更改变量类型,swift,swift2,Swift,Swift2,我有一个tableview,它希望根据数据源标识符显示不同的UITableViewCell 这是我的密码: func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { var cell : AnyObject var itemDic = items.objectAtIndex(indexPath.row) as!
tableview
,它希望根据数据源标识符显示不同的UITableViewCell
这是我的密码:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell : AnyObject
var itemDic = items.objectAtIndex(indexPath.row) as! [String : String];
switch itemDic["celltype"]! as String {
case "dash":
cell = tblGraphs.dequeueReusableCellWithIdentifier("DashboardCell") as! DashboardTableViewCell
cell.setData(itemDic, indexPath: indexPath)
case "chart":
cell = tblGraphs.dequeueReusableCellWithIdentifier("ChartCell") as! ChartTableViewCell
default:
cell = tblGraphs.dequeueReusableCellWithIdentifier("DashboardCell") as! DashboardTableViewCell
}
return cell
}
当然,这给了我一个错误:
有没有关于如何解决这个问题的线索?替换
var cell : AnyObject
与
否则,它不知道您正在尝试返回UITableViewCell
var cell : AnyObject
与
否则,它不知道您正试图返回UITableViewCell实际上,您已经获取了anyobject类型的单元格变量,但该方法返回了UITableViewCell类型的对象。
如果您有不同类型的单元格,而不是必须采用“UITableViewcell”类型的变量,那么它的工作方式将与此方法中的任何对象类似 实际上,您已经获取了anyobject类型的cell变量,但该方法返回了UITableViewcell类型的对象。
如果您有不同类型的单元格,而不是必须采用“UITableViewcell”类型的变量,那么它的工作方式将与此方法中的任何对象类似 我可能遗漏了一些内容,但为什么不只是
var-cell:UITableViewCell
呢?我可能遗漏了一些内容,但为什么不只是var-cell:UITableViewCell
呢?