实际上,我正在将我的目标c项目迁移到swift,其中我对swift也不太熟悉。我在所有columnHeights
中都收到“'AnyHashable'类型的值没有下标” 错误。基本上,该类是设计以pintrest格式显示的ui之一。此行错误-> columnHeights[section][idx] = NSNumber(value: Float(top))
Xcode = 10.3
迅速= 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))
}
}
答案 0 :(得分:0)
Swift的优点之一是,在集合中使用对象(引用类型)不是强制性的。
顾名思义,columnHeights
似乎是CGFloat
实例的嵌套数组。
声明
var columnHeights = [[CGFloat]]()
并填充
let sectionColumnHeights = [CGFloat](repeating: 0.0, count: columnCount)
columnsHeights.append(sectionColumnHeights)
在所有其他columnHeights
事件中,请删除NSNumber
对象的创建并直接使用CGFloat
答案 1 :(得分:0)
//首先,您必须了解什么是下标?
//-下标用于访问对象值而无需调用方法
这是一个小型演示,可能对保持低调很有用。
class Demo {
var testArry = ["A","B","C","D"]
subscript(_ index:Int) -> String {
return testArry[index]
}
}
let ob1 = Demo()
print(ob1[0])
print(ob1[1])