簡體   English   中英

我只想添加2個字典的值

[英]i want to add only the values of 2 dictionaries

我有一個快速代碼,該代碼會自動從目標C遷移到出現錯誤的快速位置

“二進制運算符'+'不能應用於兩個'Dictionary.Values'操作數”。

因為我試圖添加不允許的字典值。 但是如何明智地增加價值。 以下是我收到此錯誤的代碼。

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是對字典值的一種特殊的輕量級視圖 ,以避免分配額外的內存。

為了能夠連接多個值,您必須創建常規數組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM