我们有以下代码在 Swift 3 中不再工作,但在以前的 Swift 版本中工作。在代码行 array += [attributes]
中,我们收到错误“Binary Operator '+=' cannot be applied to operands of type UICollectionViewLayoutAttributes and UICollectionViewLayoutAttributes?”。任何建议:这是代码:
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var array: [UICollectionViewLayoutAttributes] = []
for i in 0 ... max(0, numberOfItems - 1) {
var indexPath = IndexPath(item: i, section: 0)
var attributes = layoutAttributesForItem(at: indexPath)
if attributes != nil {
array += [attributes]
}
}
return array
}
最佳答案
尝试使用
array.append(attributes)
代替
array += [attributes]
关于ios - 二元运算符 '+=' 不能应用于 Swift 3 中 UICollectionViewLayoutAttributes 类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44917379/