【问题标题】:Type 'Int' does not conform to protocol 'NSCopying'类型“Int”不符合协议“NSCopying”
【发布时间】:2015-08-08 16:45:41
【问题描述】:

我正在使用这个Custom Collection View Layout 实现。我可以很好地运行该 repo 中的示例,但是当我将 CustomCollectionViewLayout.swift 复制到我自己的项目中时,我在方法上收到 Type 'Int' does not conform to protocol 'NSCopying' 错误

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

其中var itemAttributesNSMutableArray!

第二个索引[indexPath.row]正在生成错误。

有什么想法吗?我尝试将相同的功能放在一个快速的操场上,并没有收到任何错误..

更新! This 是一个 swiftstub,其中保存了我的游乐场。由于Import UIKit,它没有执行,但以下原因:

let x = collectionViewLayout.itemAttributes[indexPath.section]
let y = collectionViewLayout.itemAttributes[indexPath.section][indexPath.row]
print( "x's type = \(_stdlib_getDemangledTypeName(x))")
print("x = \(x)")
print( "y's type = \(_stdlib_getDemangledTypeName(y))")
print("y = \(y)")

产量:

x's type = __NSArrayM
x = (
    "<UICollectionViewLayoutAttributes: 0x7fbe6ac041d0> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); frame = (0 0; 66 30); zIndex = 1024; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ac05030> index path: (<NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}); frame = (65 0; 63 30); zIndex = 1023; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ae1e2a0> index path: (<NSIndexPath: 0xc000000000400016> {length = 2, path = 0 - 2}); frame = (127 0; 65 30); zIndex = 1023; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ac06830> index path: (<NSIndexPath: 0xc000000000600016> {length = 2, path = 0 - 3}); frame = (191 0; 66 30); zIndex = 1023; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ae21e50> index path: (<NSIndexPath: 0xc000000000800016> {length = 2, path = 0 - 4}); frame = (256 0; 67 30); zIndex = 1023; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ad70f30> index path: (<NSIndexPath: 0xc000000000a00016> {length = 2, path = 0 - 5}); frame = (322 0; 66 30); zIndex = 1023; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ac054e0> index path: (<NSIndexPath: 0xc000000000c00016> {length = 2, path = 0 - 6}); frame = (387 0; 66 30); zIndex = 1023; ",
    "<UICollectionViewLayoutAttributes: 0x7fbe6ad72420> index path: (<NSIndexPath: 0xc000000000e00016> {length = 2, path = 0 - 7}); frame = (452 0; 65 30); zIndex = 1023; "
)
y's type = Swift.ImplicitlyUnwrappedOptional<Swift.AnyObject>
y = <UICollectionViewLayoutAttributes: 0x7fbe6ac041d0> index path: (<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}); frame = (0 0; 66 30); zIndex = 1024; 

【问题讨论】:

  • 如果将self.itemAttributes[indexPath.section]保存到变量中,Swift认为它的类型是什么?
  • @PhillipMills 我更新了我的问题。这在操场上工作但在我的编辑器中无法编译,这并不奇怪吗?
  • 您使用的是哪个版本的 XCode?​​span>
  • 我也有同样的错误。将不胜感激任何帮助。
  • Xcode 7 beta-4 @codecowboy

标签: swift nsmutablearray uicollectionviewlayout swift2 nscopying


【解决方案1】:

override func layoutAttributesForItemAtIndexPath改成

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { let row = indexPath.row let x : NSMutableArray = self.itemAttributes[indexPath.section] as! NSMutableArray let l = x[row] as? UICollectionViewLayoutAttributes return l }

我将在今天晚些时候 fork CustomCollectionLayoutView 并制作一个 swift2 副本

【讨论】:

  • 请您解释一下为什么会修复它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多