Преобразование Swift2 -> Swift3: ошибки с любым

Я использую CustomCollectionViewLayout из https://github.com/brightec/CustomCollectionViewLayout.

После преобразования из Swift2 в Swift3 возникают две ошибки относительно Any.

Ошибка1:

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
}

Ошибка сообщения:

CustomCollectionViewLayout.swift:115:54: Тип Any не имеет элементов нижнего индекса.

Ошибка 2:

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(block: { (evaluatedObject, bindings) -> Bool in
                    return rect.intersects(evaluatedObject.frame)
                })
                ) as! [UICollectionViewLayoutAttributes]


            attributes.append(contentsOf: filteredArray)

        }
    }

    return attributes
}

Ошибка сообщения:

Значение типа «Любой?» не имеет члена 'frame'

Любые идеи, как исправить проблемы с Any/AnyObject?


person Markus    schedule 14.01.2017    source источник
comment
Я также сталкиваюсь с той же проблемой   -  person Krishna Meena    schedule 13.06.2017


Ответы (1)


Я решил ошибки, замените оба метода на приведенные ниже.

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        //return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
        let arr = self.itemAttributes[indexPath.section] as! NSMutableArray
        return arr[indexPath.row] as! UICollectionViewLayoutAttributes
    }


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

                let filteredArray  =  (section as! NSMutableArray).filtered(

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


                attributes.append(contentsOf: filteredArray)

            }
        }

        return attributes
    }
person Krishna Meena    schedule 13.06.2017