【问题标题】:Binary Operator '+=' cannot be applied to operands of type UICollectionViewLayoutAttributes in Swift 3二元运算符“+=”不能应用于 Swift 3 中 UICollectionViewLayoutAttributes 类型的操作数
【发布时间】:2017-07-05 05:05:00
【问题描述】:

我们在 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
}

【问题讨论】:

  • 尝试使用array.append(attributes)

标签: ios swift3 uiviewcontroller


【解决方案1】:

尝试使用

array.append(attributes)

而不是

array += [attributes]

【讨论】:

  • 我试过这种方法。编译器现在抛出一个错误:致命错误:NSArray element failed to match the Swift Array Element type。
  • 还将var array: [UICollectionViewLayoutAttributes] = [] 更改为var array: [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
  • 这已经奏效了。非常感谢您的支持。已标记答案!
  • 很高兴为您提供帮助
【解决方案2】:

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

+= 运算符适用于 IntFloatDouble',String` 和其他不适用于数组的数据类型

【讨论】:

  • 我试过了,还是不行。我现在收到错误“可选类型的值”UICollectionViewLayoutAttributes?没有打开”。
  • 做这个 array.append(attributes!)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多