我对Swift非常陌生,正在试图将UITableViewCell强制转换为我设置的协议或基本单元类,然后使用它的方法。对我来说,我无法让编译器沉默它的警告,我不知道如何解决这个问题。下面是我采取了不同的方法:在Swift中铸造问题
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell is BaseCell {
return cell.computeHeight()
}
}
给出错误:Value of type 'UITableViewCell' has no member 'computerHeight'
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? BaseCell {
return cell.computeHeight()
}
给人以EXC_BAD_ACCESS
错误。
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
if cell.conformsToProtocol(CellDimensions) {
return cell.computeHeight()
}
}
2个误差的那一个,首先是Cannot convert value of type (CellDimension).Protocol (aka 'CellDimension.Protocol') to expected argument type 'Protocol'
,第二误差是相同的第一个例子中的误差。
任何人都有任何想法如何做到这一点? BaseCell遵循CellDimension协议,并且我得到的单元应该从BaseCell继承以允许它使用ComputeHeight,但出于某种原因,我无法在Swift上为我的生活工作。在Objective C上,这将是微不足道的,但我无法在Swift中获得正确的语法。我会很感激任何帮助!
请出示BaseCell和CellDimensions – Kametrixom
的声明第二种格式是正确的。我建议你设置一个断点并进入代码,以确定何处/为什么得到运行时异常。是的,请显示单元类和'computeHeight'方法的声明。 – Paulw11
您声明'BaseCell'遵循CellDimension协议。 BaseCell应该是 'class BaseCell:UICollectionViewCell {}' 正如Paul所说,你在表格视图中的第二个代码是正确的。 – Tim