gpt4 book ai didi

arrays - 将函数放入元组会产生 "cannot convert value of type"错误

转载 作者:行者123 更新时间:2023-11-28 06:29:01 25 4
gpt4 key购买 nike

我正在使用一个 Collection View ,我正在尝试制作一个元组数组,其中包含重用标识符、单元格的类型以及调用以在 tableView(_ :cellForItemAt:).

到目前为止一切顺利,除了功能。 Xcode 在数组的声明中提示它

"Cannot convert value of type '(AddASpotInfoVC) -> (UICollectionViewCell) -> UICollectionViewCell' to expected element type '(UICollectionViewCell) -> UICollectionViewCell'

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com