Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何在运行时更改变量类型_Swift_Swift2 - Fatal编程技术网

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
呢?