Swift 功能上有多种类型

Swift 功能上有多种类型,swift,function,class,types,Swift,Function,Class,Types,我想将typeT传递给函数(例如,它包含RatingCell),但我不明白如何将typeT精确定义为RatingCell func configureQuestionCell<T>(cellType: T, question: Question, answers: [String]?) -> UITableViewCell { let cell = self.blockContent.dequeueReusableCell(withIdentifier: Re

我想将type
T
传递给函数(例如,它包含
RatingCell
),但我不明白如何将type
T
精确定义为
RatingCell

func configureQuestionCell<T>(cellType: T, question: Question, answers: [String]?) -> UITableViewCell {

        let cell = self.blockContent.dequeueReusableCell(withIdentifier: ReusableCellID.pickerCell.rawValue) as! T
        //Value of type 'T' has no member ... 
        cell.delegate = self 
        cell.questionId = question.questionId
        cell.cellTitle.text = question.title
        cell.answerVariants = question.answerVariants 
        if answers != nil { 
            cell.userAnswers = answers!
        }
        return cell
    }
func-configureQuestionCell(单元格类型:T,问题:问题,答案:[字符串]?)->UITableViewCell{
将cell=self.blockContent.dequeueReusableCell(标识符为ReusableCellID.pickerCell.rawValue)设为!T
//类型“T”的值没有成员。。。
cell.delegate=self
cell.questionId=question.questionId
cell.cellTitle.text=question.title
cell.answerVariants=question.answerVariants
如果回答!=nil{
cell.userAnswers=答案!
}
返回单元
}

我在评论中添加了错误。我也确信
RatingCell
包含所有这些参数

我不是Swift的专业人士,但是,您是否尝试将一个接口作为参数传递,因此所有的“T”都应该包含。例如,委派
与此类似,它应该可以工作

一个可能的解决方案是添加一个协议

protocol Reusable {
    var delegate : ReusableDelegate { get set } // change the type to the real delegate type
}
并将泛型类型约束到协议。无论如何,您应该将泛型类型约束为
UITableViewCell

func configureQuestionCell<T>(cellType: T) where T : UITableViewCell & Reusable {
    let cell = self.blockContent.dequeueReusableCell(withIdentifier: ReusableCellID.ratingCell.rawValue) as! T
    cell.delegate = self
}
并在小区内采用协议和添加

typealias DelegateType = < The actual delegate type of the cell >
typealias DelegateType=

我如何使用它?我需要使用哪个委托?单元格的自定义委托类型。如果单元格类型为6-7,我需要做什么?你能给我举个例子吗?为了清楚起见,我编辑到。目前还没有决定如何指定T显式类型泛型的目标是
T
不显式。我想阅读关于泛型和协议的章节会对您有所帮助
typealias DelegateType = < The actual delegate type of the cell >