2017-01-14 32 views
2

我使用的是從https://github.com/brightec/CustomCollectionViewLayout CustomCollectionViewLayout。轉換Swift2 - > Swift3:任何錯誤

從Swift2轉換到Swift3後,出現了兩個關於Any的錯誤。

ERROR1:

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

消息錯誤:

CustomCollectionViewLayout.swift:115:54: Type 'Any' has no subscript members

錯誤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 
} 

消息錯誤:

Value of type 'Any?' has no member 'frame'

任何想法如何解決Any/AnyObject的問題?

+0

我也面臨着同樣的問題 –

回答

0

我解決了這個錯誤,請更換您的這兩種方法下面那些

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 
    }