Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类型为'的值;AnyHashable';将项目从目标c移动到swift时没有下标_Ios_Swift_Migration - Fatal编程技术网

Ios 类型为'的值;AnyHashable';将项目从目标c移动到swift时没有下标

Ios 类型为'的值;AnyHashable';将项目从目标c移动到swift时没有下标,ios,swift,migration,Ios,Swift,Migration,实际上,我正在将我的objective c项目迁移到swift,在这里我对swift也不太熟悉。我在所有的列高度中都出现了“类型为'AnyHashable'的值没有下标”的错误。基本上,这个类是设计以pintrest格式显示的ui的类之一。此行出错->columnHeights[section][idx]=NSNumber(值:Float(top)) Xcode=10.3 swift=5 // MARK: - Methods to Override override func prepare()

实际上,我正在将我的objective c项目迁移到swift,在这里我对swift也不太熟悉。我在所有的
列高度中都出现了“类型为'AnyHashable'的值没有下标”的错误。基本上,这个类是设计以pintrest格式显示的ui的类之一。此行出错->
columnHeights[section][idx]=NSNumber(值:Float(top))

Xcode=10.3
swift=5

// MARK: - Methods to Override
override func prepare() {
    super.prepare()

    headersAttribute.removeAll()
    footersAttribute.removeAll()
    unionRects.removeAll()
    columnHeights.removeAll()
    allItemAttributes.removeAll()
    sectionItemAttributes.removeAll()

    let numberOfSections = collectionView?.numberOfSections
    if numberOfSections == 0 {
        return
    }

    assert(delegate is CHTCollectionViewDelegateWaterfallLayout ?? false, "UICollectionView's delegate should conform to CHTCollectionViewDelegateWaterfallLayout protocol")
    assert(self.columnCount > 0 || delegate?.responds(to: #selector(CHTCollectionViewDelegateWaterfallLayout.collectionView(_:layout:columnCountForSection:))) ?? false, "UICollectionViewWaterfallLayout's columnCount should be greater than 0, or delegate must implement columnCountForSection:")

    // Initialize variables
    var idx = 0

    for section in 0..<(numberOfSections ?? 0) {
        let columnCount = self.columnCount(forSection: section)
        var sectionColumnHeights = [AnyHashable](repeating: 0, count: columnCount)
        for idx in 0..<columnCount {
            sectionColumnHeights.append(NSNumber(value: 0))
        }
        columnHeights.append(sectionColumnHeights)
    }
    // Create attributes
    var top: CGFloat = 0
    var attributes: UICollectionViewLayoutAttributes?

    for section in 0..<(numberOfSections ?? 0) {
        /*
         * 1. Get section-specific metrics (minimumInteritemSpacing, sectionInset)
         */
        var minimumInteritemSpacing: CGFloat
        if delegate?.responds(to: #selector(UICollectionViewDelegateFlowLayout.collectionView(_:layout:minimumInteritemSpacingForSectionAt:))) ?? false {
            if let collectionView = collectionView {
                minimumInteritemSpacing = delegate?.collectionView?(collectionView, layout: self, minimumInteritemSpacingForSectionAt: section) ?? 0.0
            }
        } else {
            minimumInteritemSpacing = self.minimumInteritemSpacing
        }

        var columnSpacing = minimumColumnSpacing
        if delegate?.responds(to: #selector(CHTCollectionViewDelegateWaterfallLayout.collectionView(_:layout:minimumColumnSpacingForSectionAt:))) ?? false {
            columnSpacing = delegate?.collectionView?(collectionView, layout: self, minimumColumnSpacingForSectionAt: section) ?? 0.0
        }

        var sectionInset: UIEdgeInsets
        if delegate?.responds(to: #selector(UICollectionViewDelegateFlowLayout.collectionView(_:layout:insetForSectionAt:))) ?? false {
            if let collectionView = collectionView, let collection = delegate?.collectionView?(collectionView, layout: self, insetForSectionAt: section) {
                sectionInset = collection
            }
        } else {
            sectionInset = self.sectionInset
        }

        let width = (collectionView?.bounds.size.width ?? 0.0) - sectionInset.left - sectionInset.right
        let columnCount = self.columnCount(forSection: section)
        let itemWidth = CHTFloorCGFloat((width - CGFloat((columnCount - 1)) * columnSpacing) / CGFloat(columnCount))

        /*
         * 2. Section header
         */
        var headerHeight: CGFloat
        if delegate?.responds(to: #selector(CHTCollectionViewDelegateWaterfallLayout.collectionView(_:layout:heightForHeaderInSection:))) ?? false {
            headerHeight = delegate?.collectionView?(collectionView, layout: self, heightForHeaderInSection: section) ?? 0.0
        } else {
            headerHeight = self.headerHeight
        }

        var headerInset: UIEdgeInsets
        if delegate?.responds(to: #selector(CHTCollectionViewDelegateWaterfallLayout.collectionView(_:layout:insetForHeaderInSection:))) ?? false {
            if let collection = delegate?.collectionView?(collectionView, layout: self, insetForHeaderInSection: section) {
                headerInset = collection
            }
        } else {
            headerInset = self.headerInset
        }

        top += headerInset.top

        if headerHeight > 0 {
            attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: CHTCollectionElementKindSectionHeader, with: IndexPath(item: 0, section: section))
            attributes?.frame = CGRect(x: headerInset.left, y: top, width: (collectionView?.bounds.size.width ?? 0.0) - (headerInset.left + headerInset.right), height: headerHeight)

            if let attributes = attributes {
                headersAttribute[NSNumber(value: section)] = attributes
            }
            if let attributes = attributes {
                allItemAttributes.append(attributes)
            }

            top = attributes?.frame.maxY ?? 100 + headerInset.bottom
        }

        top += sectionInset.top
        for idx in 0..<columnCount {
           columnHeights[section][idx] = NSNumber(value: Float(top))

        }

        /*
         * 3. Section items
         */
        let itemCount = collectionView?.numberOfItems(inSection: section)
        var itemAttributes = [AnyHashable](repeating: 0, count: itemCount ?? 0)

        // Item will be put into shortest column.
        for idx in 0..<(itemCount ?? 0) {
            let indexPath = IndexPath(item: idx, section: section)
            let columnIndex = nextColumnIndex(forItem: idx, inSection: section)
            let xOffset = sectionInset.left + (itemWidth + columnSpacing) * CGFloat(columnIndex)
           swift let yOffset = CGFloat((columnHeights[section][columnIndex] as? NSNumber)?.floatValue)
            var itemSize: CGSize? = nil
            if let collectionView = collectionView {
                itemSize = delegate?.collectionView?(collectionView, layout: self, sizeForItemAt: indexPath)
            }
            var itemHeight: CGFloat = 0
            if (itemSize?.height ?? 0.0) > 0 && (itemSize?.width ?? 0.0) > 0 {
                itemHeight = CHTFloorCGFloat((itemSize?.height ?? 0.0) * itemWidth / (itemSize?.width ?? 0.0))
            }

            attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes?.frame = CGRect(x: xOffset, y: yOffset, width: itemWidth, height: itemHeight)
            if let attributes = attributes {
                itemAttributes.append(attributes)
            }
            if let attributes = attributes {
                allItemAttributes.append(attributes)
            }
            columnHeights[section][columnIndex] = NSNumber(value: Float(attributes?.frame.maxY + minimumInteritemSpacing))
        }

        sectionItemAttributes.append(itemAttributes)

        /*
         * 4. Section footer
         */
        var footerHeight: CGFloat
        let columnIndex = longestColumnIndex(inSection: section)
        if ((columnHeights[section] as? [Any])?.count ?? 0) > 0 {
            top = CGFloat((columnHeights[section][columnIndex] as? NSNumber)?.floatValue) - minimumInteritemSpacing + sectionInset.bottom
        } else {
            top = 0
        }

        if delegate?.responds(to: #selector(CHTCollectionViewDelegateWaterfallLayout.collectionView(_:layout:heightForFooterInSection:))) ?? false {
            footerHeight = delegate?.collectionView?(collectionView, layout: self, heightForFooterInSection: section) ?? 0.0
        } else {
            footerHeight = self.footerHeight
        }

        var footerInset: UIEdgeInsets
        if delegate?.responds(to: #selector(CHTCollectionViewDelegateWaterfallLayout.collectionView(_:layout:insetForFooterInSection:))) ?? false {
            if let collection = delegate?.collectionView?(collectionView, layout: self, insetForFooterInSection: section) {
                footerInset = collection
            }
        } else {
            footerInset = self.footerInset
        }

        top += footerInset.top

        if footerHeight > 0 {
            attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: CHTCollectionElementKindSectionFooter, with: IndexPath(item: 0, section: section))
            attributes?.frame = CGRect(x: footerInset.left, y: top, width: (collectionView?.bounds.size.width ?? 0.0) - (footerInset.left + footerInset.right), height: footerHeight)

            if let attributes = attributes {
                footersAttribute[NSNumber(value: section)] = attributes
            }
            if let attributes = attributes {
                allItemAttributes.append(attributes)
            }

            top = attributes?.frame.maxY + footerInset.bottom
        }

        for idx in 0..<columnCount {
            columnHeights[section][idx] = NSNumber(value: Float(top))
        }
        // end of for (NSInteger section = 0; section < numberOfSections; ++section)
    }

    // Build union rects
    idx = 0
    let itemCounts = allItemAttributes.count
    while idx < itemCounts {
        var unionRect = (allItemAttributes[idx] as? UICollectionViewLayoutAttributes)?.frame
        let rectEndIndex = min(idx + unionSize, itemCounts)

        for i in idx + 1..<rectEndIndex {
            unionRect = unionRect?.union((allItemAttributes[i] as? UICollectionViewLayoutAttributes)?.frame)
        }

        idx = rectEndIndex

        unionRects.append(NSValue(cgRect: unionRect ?? CGRect.zero))
    }
}
//标记:-要重写的方法
重写func prepare(){
超级准备
headersAttribute.removeAll()
footersAttribute.removeAll()
unionRects.removeAll()
columnHeights.removeAll()
allItemAttributes.removeAll()
sectionItemAttributes.removeAll()
设numberOfSections=collectionView?.numberOfSections
如果numberOfSections==0{
返回
}
断言(委托为CHTCollectionViewDelegateWaterWallLayout??false,“UICollectionView的委托应符合CHTCollectionViewDelegateWaterWallLayout协议”)
断言(self.columnCount>0 | | | delegate?。响应:#选择器(chtCollectionViewDelegateWaterWallLayout.collectionView(U2;:layout:columnCountForSection:))?false,“UICollectionViewWaterWallLayout的columnCount应该大于0,或者委托必须实现columnCountForSection:)
//初始化变量
var idx=0
对于0..0中的节{
attributes=UICollectionViewLayoutAttributes(对于种类为:CHTCollectionElementKindSectionFooter的SupplementViewofKind,带有:IndexPath(项:0,节:节))
attributes?.frame=CGRect(x:footerInset.left,y:top,width:(collectionView?.bounds.size.width±0.0)-(footerInset.left+footerInset.right),高度:footerHeight)
如果let attributes=attributes{
footersAttribute[NSNumber(值:节)]=属性
}
如果let attributes=attributes{
allItemAttributes.append(属性)
}
top=属性?.frame.maxY+footerInset.bottom
}

对于0..中的idx,Swift的一个好处是在集合中不必使用对象(引用类型)

顾名思义,
columnHeights
似乎是
CGFloat
实例的嵌套数组

申报

var columnHeights = [[CGFloat]]()
并填充它

let sectionColumnHeights = [CGFloat](repeating: 0.0, count: columnCount)
columnsHeights.append(sectionColumnHeights)

在所有其他出现的
columnHeights
中,删除创建
NSNumber
对象,并直接使用
CGFloat

//首先您必须了解什么是下标

//-下标用于在不调用方法的情况下访问对象值

这是一个小演示,可能有助于理解。

class Demo {
  var testArry = ["A","B","C","D"]
  subscript(_ index:Int) -> String {
    return testArry[index]
  }
}
let ob1 = Demo()
print(ob1[0])
print(ob1[1])