简体   繁体   English

模糊地使用'下标' - ios 9 Swift 2.0

[英]Ambiguous use of 'subscript' - ios 9 Swift 2.0

I was writing ios app using Swift 2.0 on xcode 7.0. 我在xcode 7.0上使用Swift 2.0编写ios app。 Before updating to the last version of xCode 7.1 the same exact code was working perfectly 在更新到最新版本的xCode 7.1之前,完全相同的代码完全正常

After the update i got this error: 更新后,我收到此错误:

Ambiguous use of 'subscript' 模糊地使用'下标'

in those lines: 在这些方面:

  override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> (UICollectionViewLayoutAttributes!) {
    return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
  }

this is the full class: 这是全班:

class CustomCollectionViewLayout: UICollectionViewLayout {

  var numberOfColumns = 7 // the number of columns
  var itemAttributes : NSMutableArray!
  var itemsSize : NSMutableArray!
  var contentSize : CGSize!

  func setColumnNumber(columnNum: Int) {
    numberOfColumns = columnNum
  }

  override func prepareLayout() {
    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!.numberOfItemsInSection(section)
        for index in 0..<numberOfItems {
          if section != 0 && index != 0 {
            continue
          }

          let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: index, inSection: 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].CGSizeValue()
        let indexPath = NSIndexPath(forItem: index, inSection: section)
        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        attributes.frame = CGRectIntegral(CGRectMake(xOffset, yOffset, itemSize.width, itemSize.height))

        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.addObject(attributes)

        xOffset += itemSize.width
        column++

        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 .addObject(sectionAttributes)
    }

    let attributes : UICollectionViewLayoutAttributes = self.itemAttributes.lastObject?.lastObject as! UICollectionViewLayoutAttributes
    contentHeight = attributes.frame.origin.y + attributes.frame.size.height
    if( contentWidth == 0 || contentHeight == 0){return;}
    self.contentSize = CGSizeMake(contentWidth, contentHeight)
  }

  override func collectionViewContentSize() -> CGSize {
    if( self.contentSize != nil){
      return self.contentSize
    }else {
      return CGSizeMake(0, 0)
    }
  }

  override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> (UICollectionViewLayoutAttributes!) {
    return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes
  }

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

        let filteredArray  =  section.filteredArrayUsingPredicate(

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


        attributes.appendContentsOf(filteredArray)

      }
    }

    return attributes
  }

  override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
    return true
  }

  func sizeForItemWithColumnIndex(columnIndex: Int) -> CGSize {
    let text : String = "25.10.15"
    let size : CGSize = (text as NSString).sizeWithAttributes([NSFontAttributeName: UIFont.systemFontOfSize(17.0)])
    let width : CGFloat = size.width + 25
    return CGSizeMake(width, 30)
  }

  func calculateItemsSize() {
    self.itemsSize = NSMutableArray(capacity: numberOfColumns)
    for index in 0..<numberOfColumns {
      self.itemsSize.addObject(NSValue(CGSize: self.sizeForItemWithColumnIndex(index)))
    }
  }
}

The original library 原来的图书馆

The compiler doesn't know what is being returned by self.itemAttributes[indexPath.section] since its defined as an NSMutableArray . 编译器不知道self.itemAttributes[indexPath.section]返回的内容,因为它定义为NSMutableArray Instead you should define itemAttributes as an array of array of UICollectionViewLayoutAttributes which is what it looks like you've got. 相反,你应该定义itemAttributes的数组的数组UICollectionViewLayoutAttributes是什么样子,你已经得到了。 So itemAttributes: [[UICollectionViewLayoutAttributes]] should take care of that warning and would be the preferred way of writing that in Swift. 所以itemAttributes: [[UICollectionViewLayoutAttributes]]应该处理那个警告,并且是在Swift中编写它的首选方式。

Edit: You should also redefine sectionAttributes as [UICollectionViewLayoutAttributes] . 编辑:你也应该重新定义sectionAttributes[UICollectionViewLayoutAttributes] So now the compiler can fully infer the type of object being returned for the subscripting. 所以现在编译器可以完全推断为下标返回的对象类型。

As for why it changed in the recent release, I'm not sure, I do not see anything about this specifically in the release notes. 至于为什么它在最近的版本中发生了变化,我不确定,我在发行说明中没有看到任何有关此内容的内容。

Referring 'Peter Foti' answer , I have changed code to 引用'Peter Foti'的回答,我已将代码更改为

let sectionAttributes = self.itemAttributes [indexPath.section] as! [UICollectionViewLayoutAttributes]
return sectionAttributes[indexPath.row] as UICollectionViewLayoutAttributes

Instead of line with error 'Ambiguous use of 'subscript' 而不是错误地'错误地使用'下标'

return self.itemAttributes[indexPath.section][indexPath.row] as! UICollectionViewLayoutAttributes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM