【问题标题】:NSMutableArray cast to swift Array of custom typeNSMutableArray 转换为自定义类型的快速数组
【发布时间】:2014-12-04 23:21:46
【问题描述】:

我想弄清楚如何将 NSMutableArray 指定或强制转换为自定义类型的 swift Array。我目前有 2 个文件:

  1. 第一个文件需要一个 NSMutableArray 来实现其功能(通过引用传递,能够删除具有我不知道的索引的特定对象)
  2. 第二个文件使用 Swift 数组(更好的内存/一次性数组),具有自定义类型,我声明使用

let newArray: [CustomType]!

我需要将 NSMutableArray 作为参数传递给第二个文件中的函数,该函数需要 [CustomType]。简单地调用它时:

let newVC = UIViewController(array: mutableArray)

它一直告诉我'CustomType' is not identical to 'AnyObject'。我尝试使用mutableArray as [CustomType] 调用该函数,它也不起作用。如何让 swift Array 函数接受我的 NSMutableArray?

【问题讨论】:

标签: ios objective-c arrays swift


【解决方案1】:

这对我有用:

var swiftArray = NSArray(array:mutableArray) as Array<CustomType>

swiftArray 是一个自定义类型对象的 Swift 数组。您可以按预期传递数组并对其进行迭代。

【讨论】:

  • 请注意,这仅在您的 NSMutableArray 包含 CustomType 对象时才有效。
【解决方案2】:

我需要的是这样的:

让 newVC = UIViewController(array: mutableArray as AnyObject as [CustomType])

【讨论】:

  • 酷,语法非常紧凑。
猜你喜欢
  • 2019-03-30
  • 2016-10-19
  • 2016-07-02
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-25
  • 2014-11-08
相关资源
最近更新 更多