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

标签 ios swift3 uiviewcontroller

我们有以下代码在 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/

相关文章:

ios - 在 Swift 中将图像从 ViewController 传递到 TabBarController 的 ViewController 子级

iphone - NSManagedObject 中的 Setter 有选择地失败

ios - IOS 9.1/Xcode 7.1.1 中的蓝牙损坏

ios - 将数据从 View Controller 传递到 Cocos2D 场景

ios - 我无法从 PFFile 获取图像

IOS swift如何知道activityViewController何时被成功使用

ios - Collection View 单元格的多项选择

IOS Swift 如何获取 View 1 到 View 2 上的 SearchBar 文本值

ios - 使用 Save 函数更新 ViewController 中的文本

ios - 无法调用非函数类型的值 "UIViewController?"