2016-09-09 6 views
0

を与える私は、迅速な3に迅速2から私のプロジェクトを変換する、と私は次の関数と間違って何を理解困難を持っている:layoutAttributesForElements関数がエラー警告

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は以下のように定義クラスのプロパティです。

var itemAttributes : NSMutableArray!

sectionAttributesは別の機能に設定されている:

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がスイフト3において

return rect.intersects(evaluatedObject.frame)
+0

あなたの 'itemAttributes'(または' sectionAttributes')には何が入っていますか?それがあなたの問題の重要な部分です。できるだけ正確に 'itemAttributes'と' sectionAttributes'を記述する必要があります。 – OOPer

+0

質問を更新し、問題を再現可能なコードにしてください。 – OOPer

+0

私は質問を更新しています。コメント内のコードスニペットは理解しづらいものでした。 –

答えて

5

ラインにおけるlayoutAttributesForElements関数のType of expression is ambiguous without more context をスロー、NSMutableArray(又はNSArrayも)の値の型は、で動作するように硬いものである、Anyとなっています。

できるだけ早く配列タイプをSwift Arrayにキャストする方がよいでしょう。

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 
} 

をしかし、私はあなたが限りすることができますようスウィフトネイティブArray秒またはDictionary Sを使用することをお勧めします。

これを試してみてください。

var itemAttributes: [[UICollectionViewLayoutAttributes]] = [] 

     var sectionAttributes: [UICollectionViewLayoutAttributes] = [] 

(。これで、あなたには、いくつかのより多くの修正が必要になることがありますが、あなたはすぐにそれらを見つけることができます)

+0

はい、これはエラーの原因となりました。ありがとうございました! –

0

OOPerの助けを借りて、私は、コードを次のように変更作られた:

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 
} 

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) 
    } 
} 

var itemAttributes : [[UICollectionViewLayoutAttributes]]!