【发布时间】:2021-07-01 06:49:33
【问题描述】:
在 collectionView 中选择时在数组中添加模型对象,现在需要从数组中删除取消选择的项目。但是 where 子句不起作用,因为它是在数组中添加的模型,而不是任何正常的数据类型。请指导。以下是我的代码:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if selectedIndex.contains(indexPath.row) {
selectedIndex.removeAll(where: { $0 == indexPath.row })
if(userCategories.count > 0){
userCategories.removeAll(where: { $0 == self.categoriesList[indexPath.row] }) // Error in this line
print(userCategories)
}
}else {
selectedIndex.append(indexPath.row)
userCategories.append(self.categoriesList[indexPath.row])
print(userCategories)
}
}
错误是:
Binary operator '==' cannot be applied to two 'CategoriesData' operands
模型类:
class CategoriesData: Codable {
var id: Int?
var name: String?
var status: String?
}
【问题讨论】:
-
显示存储在 userCategories 数组中的模型
-
类 CategoriesData: Codable { var id: Int?变量名称:字符串?变量状态:字符串?
标签: ios arrays swift xcode uicollectionview