我在 xcode 7.0上使用 Swift 2.0编写ios app.在更新到最新版本的xCode 7.1之前,完全相同的代码完全正常

更新后,我收到此错误:

Ambiguous use of ‘subscript’

在这些方面:

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

这是全班:

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

解决方法

编译器不知道self.itemAttributes [indexPath.section]返回的内容,因为它定义为NSMutableArray.相反,你应该将itemAttributes定义为UICollectionViewLayoutAttributes数组的数组,这就是你所拥有的.所以itemAttributes:[[UICollectionViewLayoutAttributes]]应该处理那个警告,并且是在Swift中编写它的首选方式.

编辑:您还应该将sectionAttributes重新定义为[UICollectionViewLayoutAttributes].所以现在编译器可以完全推断为下标返回的对象类型.

至于为什么它在最近的版本中发生了变化,我不确定,我在发行说明中没有看到任何有关此内容的内容.

模糊地使用’下标’ – ios 9 Swift 2.0的更多相关文章

  1. ios – Swift相当于`[NSDictionary initWithObjects:forKeys:]`

    Swift的原生字典是否与[NSDictionaryinitWithObjects:forKeys:]相当?假设我有两个带键和值的数组,并希望将它们放在字典中.在Objective-C中,我这样做:当然我可以通过两个数组迭代一个计数器,使用vardict:[String:Int]并逐步添加东西.但这似乎不是一个好的解决方案.使用zip和enumerate可能是同时迭代两者的更好方法.然而,这种方法

  2. nsmutablearray – Sprite Kit iOS7 – SKNode UserData属性不存储值

    谢谢解决方法userData属性最初为零.您必须先创建一个字典并进行分配:

  3. ios – 在Swift中获取Cocoa Touch Framework项目版本字符串

    有谁知道这是否是我的项目设置中的缺陷,Xcode中的一个错误,或者是否有一种方法可以将Swift中的框架版本作为String或数组获取,这样我可以提供比major.minor更精细的版本控制?

  4. ios – 搜索数组swift中的对象

    我正在尝试使用UISearchController创建搜索功能.但是,我似乎无法使其与我的团队对象一起工作.我首先创建了一个包含id,name和shortname的TeamObject.然后我从一个url中检索teamData,并将TeamObjects添加到一个填充到tableView中的数组中.这个tableView包含一个searchController,它假设过滤数据,但没有任何反应.阵列

  5. ios – UITableView reloadData什么都不做(UITableView不是nil)

    如果您需要查看更多我的代码,请告诉我.我也看了一下beginUpdates和endUpdates的方法,但在我看来,他们一度关注一些变化和用户交互性.我想根据用户选择重新加载整个表.或者还有另一种更好的方法吗?如果没有,请仔细检查并验证myTableView委托是否设置正确.如果您已正确地将表连接到笔尖并且您有一个插座,您也可以在代码中设置它,即在viewDidLoad方法中,通过设置:

  6. ios – Swift可选项:语言问题,还是做错了什么?

    应该有可选的类型;type是但是,如果我这样做,它的工作原理:它似乎是基本的替代,但我可能会遗漏一些语言的细微差别.谁能对此有所了解?之后就像暧昧一样,更多,这是我的解决方案:这适用于所有非对象Swift对象,包括Swift字符串,数字等.感谢Viktor提醒我String不是Swift中的对象.如果您知道值的类型,您可以替换任何?使用适当的可选类型,如String?

  7. ios – Swift – 使用字典数组从字典访问数据时出错

    我有一个非常简单的例子,说明我想做什么基本上,我有一个字典,其值包含[String:String]字典数组.我把数据填入其中,但当我去访问数据时,我收到此错误:Cannotsubscriptavalueoftype‘[([String:String])]?’withanindexoftype‘Int’请让我知道我做错了什么.解决方法您的常量数组是可选的.订阅字典总是返回一个可选项.你必须打开它.更

  8. ios – 在Swift中使用“Map”创建两个数组的超集

    假设我有两个数组:我想组合两个数组,以便我得到一个输出我该怎么做呢?

  9. 模糊地使用’下标’ – ios 9 Swift 2.0

    我在xcode7.0上使用Swift2.0编写iosapp.在更新到最新版本的xCode7.1之前,完全相同的代码完全正常更新后,我收到此错误:Ambiguoususeof‘subscript’在这些方面:这是全班:Theoriginallibrary解决方法编译器不知道self.itemAttributes[indexPath.section]返回的内容,因为它定义为NSMutableArray

  10. xcode – Swift – 检索子视图

    解决方法UIViewController没有子视图属性.它有一个view属性,它有一个subviews属性:但通常这不是一个好主意.您应该将所需的标签放入IBOutletCollection并迭代它.否则,您与确切的子视图集密切相关.要创建IBOutletCollection,请在IB中选择所需的所有标签,然后将其控制拖动到源代码中.它应该询问您是否要创建一个集合数组.

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部