我正在使用集合 View ,并且试图创建一个元组数组,其中包含重用标识符,单元格的类型以及用于在tableView(_:cellForItemAt:)
中设置单元格的函数。
到目前为止,除了功能之外,一切都很好。 Xcode在数组的声明中抱怨它
AddASpotInfoVC
是 View Controller 的类。这是数组的初始化本身:
typealias cellFunc = (_ aCell: UICollectionViewCell) -> UICollectionViewCell
let reuseIdentifiers: [(String, UICollectionViewCell.Type, cellFunc)] = [
("AddASpotInfoPicCell", AddASpotInfoPicCell.self, picCellFunc),
("AddASpotInfoNameCell", AddASpotInfoNameCell.self, nameCellFunc),
("AddASpotInfoDescCell", AddASpotInfoDescCell.self, descCellFunc),
("AddASpotInfoTagsCell", AddASpotInfoTagsCell.self, tagCellFunc)]
并且数组中的所有函数(目前)都在 View Controller 类的扩展中(我认为这不是相关的……),看起来像这样
func picCellFunc(aCell: UICollectionViewCell) -> UICollectionViewCell {
return aCell
}
那么,我在数组初始化中做错了什么?
最佳答案
函数的实现可以依赖于其他变量。因此,它要求您将函数声明为静态。采用:
static func picCellFunc (aCell: UICollectionViewCell) -> UICollectionViewCell
在数组内部使用:
AddASpotInfoVC.picCellFunc
编辑:您还可以将数组初始化代码移到您的函数或初始化程序之一中。
关于arrays - 将函数放入元组会产生 “cannot convert value of type”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40940741/