gpt4 book ai didi

ios - 我只想添加 2 个字典的值

转载 作者:行者123 更新时间:2023-11-28 11:27:13 25 4
gpt4 key购买 nike

我有一个 swift 代码,它自动从 objective C 迁移到 swift 哪里出错了

"Binary operator '+' cannot be applied to two 'Dictionary.Values' operands".

因为我试图添加不允许的字典值。但是如何明智地增加其他人的值(value)。下面是我收到此错误的代码。

var result = cellAttrDict.values + supplHeaderAttrDict.values

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var i: Int
var begin = 0
var end = unionRects.count
var cellAttrDict: [AnyHashable : Any] = [:]
var supplHeaderAttrDict: [AnyHashable : Any] = [:]
var supplFooterAttrDict: [AnyHashable : Any] = [:]
var decorAttrDict: [AnyHashable : Any] = [:]

for i in 0..<unionRects.count {
if rect.intersects(unionRects[i] as! CGRect) {
begin = i * unionSize
break
}
}
i = unionRects.count - 1
while i >= 0 {
if rect.intersects(unionRects[i] as! CGRect) {
end = min((i + 1) * unionSize, allItemAttributes.count)
break
}
i -= 1
}
for i in begin..<end {
let attr = allItemAttributes[i] as? UICollectionViewLayoutAttributes
if rect.intersects(attr?.frame ?? 0 as! CGRect) {
switch attr?.representedElementCategory {
case .supplementaryView?:
if (attr?.representedElementKind == CHTCollectionElementKindSectionHeader) {
if let indexPath = attr?.indexPath, let attr = attr {
supplHeaderAttrDict[indexPath] = attr
}
} else if (attr?.representedElementKind == CHTCollectionElementKindSectionFooter) {
if let indexPath = attr?.indexPath, let attr = attr {
supplFooterAttrDict[indexPath] = attr
}
}
case .decorationView?:
if let indexPath = attr?.indexPath, let attr = attr {
decorAttrDict[indexPath] = attr
}
case .cell?:
if let indexPath = attr?.indexPath, let attr = attr {
cellAttrDict[indexPath] = attr
}
@unknown default:
break
}
}
}

var result = cellAttrDict.values + supplHeaderAttrDict.values
result = result + supplFooterAttrDict.values
result = result + decorAttrDict.values
return result as? [UICollectionViewLayoutAttributes]
}

最佳答案

Dictionary.Values 是字典值的特殊轻量级 View ,以避免分配额外的内存。

为了能够连接多个值,您必须创建常规数组

var result = Array(cellAttrDict.values) + Array(supplHeaderAttrDict.values)
result += Array(supplFooterAttrDict.values)
result += Array(decorAttrDict.values)
return result as? [UICollectionViewLayoutAttributes]

关于ios - 我只想添加 2 个字典的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57850166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com