Ivan C Myrvold
Ivan C Myrvold

Reputation: 840

layoutAttributesForElements function from swift 2 gives error warning

I am converting my project from swift 2 to swift 3, and I have difficulties understanding what's wrong with the following function:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
   var attributes = [UICollectionViewLayoutAttributes]()
    if self.itemAttributes != nil {
        for section in self.itemAttributes {

            let filteredArray  =  (section as AnyObject).filtered(

                using: NSPredicate { (evaluatedObject, bindings) -> Bool in
                    return rect.intersects(evaluatedObject.frame)
                }
            ) as! [UICollectionViewLayoutAttributes]

            attributes.append(contentsOf: filteredArray)

        }
    }

   return attributes
}

self.itemAttributes is a class property defined as:

var itemAttributes : NSMutableArray!

sectionAttributes is set in another function:

override func prepare() {
  if self.collectionView?.numberOfSections == 0 {
        return
    }

    if (self.itemAttributes != nil && self.itemAttributes.count > 0) {
        for section in 0..<self.collectionView!.numberOfSections {
            let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section)
           for index in 0..<numberOfItems {
                if section != 0 && index != 0 {
                    continue
                }

                let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section))!
                if section == 0 {
                    var frame = attributes.frame
                    frame.origin.y = self.collectionView!.contentOffset.y
                    attributes.frame = frame
                }

                if index == 0 {
                    var frame = attributes.frame
                    frame.origin.x = self.collectionView!.contentOffset.x
                    attributes.frame = frame
                }
            }
        }
        return
    }

    if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) {
        self.calculateItemsSize()
    }

    var column = 0
    var xOffset : CGFloat = 0
    var yOffset : CGFloat = 0
    var contentWidth : CGFloat = 0
    var contentHeight : CGFloat = 0

    for section in 0..<self.collectionView!.numberOfSections {
        let sectionAttributes = NSMutableArray()

        for index in 0..<numberOfColumns {
            let itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue
            let indexPath = IndexPath(item: index, section: section)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral

            if section == 0 && index == 0 {
                attributes.zIndex = 1024;
            } else  if section == 0 || index == 0 {
                attributes.zIndex = 1023
            }

            if section == 0 {
                var frame = attributes.frame
                frame.origin.y = self.collectionView!.contentOffset.y
                attributes.frame = frame
            }
            if index == 0 {
                var frame = attributes.frame
                frame.origin.x = self.collectionView!.contentOffset.x
                attributes.frame = frame
            }

            sectionAttributes.add(attributes)

            xOffset += (itemSize?.width)!
            column += 1

            if column == numberOfColumns {
                if xOffset > contentWidth {
                    contentWidth = xOffset
                }

                column = 0
                xOffset = 0
                yOffset += (itemSize?.height)!
            }
        }
        if (self.itemAttributes == nil) {
            self.itemAttributes = NSMutableArray(capacity: self.collectionView!.numberOfSections)
        }
        self.itemAttributes .add(sectionAttributes)
    }

    let attributes : UICollectionViewLayoutAttributes = (self.itemAttributes.lastObject as AnyObject).lastObject as! UICollectionViewLayoutAttributes
    contentHeight = attributes.frame.origin.y + attributes.frame.size.height
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)}

Xcode throws a Type of expression is ambiguous without more context for the layoutAttributesForElements function in the return rect.intersects(evaluatedObject.frame) line

Upvotes: 1

Views: 4810

Answers (2)

Ivan C Myrvold
Ivan C Myrvold

Reputation: 840

With the help of OOPer I made the following changes to the code:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var attributes = [UICollectionViewLayoutAttributes]()
    if let itemAttributes = self.itemAttributes {
        for section in itemAttributes {
            let filteredArray  =  section.filter{ evaluatedObject in
                return rect.intersects(evaluatedObject.frame)
                }
            attributes.append(contentsOf: filteredArray)
        }
    }

    return attributes
}

and

override func prepare() {
    if self.collectionView?.numberOfSections == 0 {
        return
    }

    if (self.itemAttributes.count > 0) {
        for section in 0..<self.collectionView!.numberOfSections {
            let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section)
            for index in 0..<numberOfItems {
                if section != 0 && index != 0 {
                    continue
                }

                let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section))!
                if section == 0 {
                    var frame = attributes.frame
                    frame.origin.y = self.collectionView!.contentOffset.y
                    attributes.frame = frame
                }

                if index == 0 {
                    var frame = attributes.frame
                    frame.origin.x = self.collectionView!.contentOffset.x
                    attributes.frame = frame
                }
            }
        }
        return
    }

    if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) {
        self.calculateItemsSize()
    }

    var column = 0
    var xOffset : CGFloat = 0
    var yOffset : CGFloat = 0
    var contentWidth : CGFloat = 0
    var contentHeight : CGFloat = 0

    for section in 0..<self.collectionView!.numberOfSections {
        var sectionAttributes: [UICollectionViewLayoutAttributes] = []

        for index in 0..<numberOfColumns {
            let itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue
            let indexPath = IndexPath(item: index, section: section)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral

            if section == 0 && index == 0 {
                attributes.zIndex = 1024;
            } else  if section == 0 || index == 0 {
                attributes.zIndex = 1023
            }

            if section == 0 {
                var frame = attributes.frame
                frame.origin.y = self.collectionView!.contentOffset.y
                attributes.frame = frame
            }
            if index == 0 {
                var frame = attributes.frame
                frame.origin.x = self.collectionView!.contentOffset.x
                attributes.frame = frame
            }

            sectionAttributes.append(attributes)

            xOffset += (itemSize?.width)!
            column += 1

            if column == numberOfColumns {
                if xOffset > contentWidth {
                    contentWidth = xOffset
                }

                column = 0
                xOffset = 0
                yOffset += (itemSize?.height)!
            }
        }
        self.itemAttributes.append(sectionAttributes)
    }

    if let attributes = self.itemAttributes.last?.last {
        contentHeight = attributes.frame.origin.y + attributes.frame.size.height
        self.contentSize = CGSize(width: contentWidth, height: contentHeight)
    }
}

and

var itemAttributes : [[UICollectionViewLayoutAttributes]]!

Upvotes: 0

OOPer
OOPer

Reputation: 47886

In Swift 3, the value type of NSMutableArray (or NSArray as well) has become Any, which is a hard thing to work with.

You'd better cast your Array types to Swift Array, as fast as you can.

Try this:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var attributes = [UICollectionViewLayoutAttributes]()
    if let itemAttributes = self.itemAttributes as NSArray as? [[UICollectionViewLayoutAttributes]] {
        for section in itemAttributes {
            let filteredArray = section.filter {evaluatedObject in
                return rect.intersects(evaluatedObject.frame)
            }
            attributes.append(contentsOf: filteredArray)
        }
    }
    return attributes
}

But I recommend you to use Swift native Arrays or Dictionarys as far as you can.

var itemAttributes: [[UICollectionViewLayoutAttributes]] = []

       var sectionAttributes: [UICollectionViewLayoutAttributes] = []

(With this, you may need some more fixes, but you'll soon find them.)

Upvotes: 5

Related Questions