二进制运算符'+ ='不能应用于Swift 3中的UICollectionViewLayoutAttributes类型的操作数

时间:2017-07-05 05:05:01

标签: ios swift3 uiviewcontroller

我们在Swift 3中有以下代码不再有效但在早期版本的Swift中工作。在代码行array += [attributes]中我们得到错误“二进制运算符'+ ='不能应用于UICollectionViewLayoutAttributes和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
}

2 个答案:

答案 0 :(得分:1)

尝试使用

array.append(attributes)

而不是

array += [attributes]

答案 1 :(得分:1)

尝试使用array.append(attributes)。 因为要在数组中添加内容,我们使用append函数

+ =运算符适用于IntFloatDouble',字符串`以及不适用于数组的其他数据类型