如何将一组快速元组转换为NSMutableArray?

时间:2021-07-06 16:00:51

I have swift array of tuples [(String, String)] and would like to cast this array to NSMutableArray. I have tried this and it is not working:

我有快速的元组[(String,String)]数组,并希望将此数组转换为NSMutableArray。我试过这个并没有用:

let myNSMUtableArray = swiftArrayOfTuples as! AnyObject as! NSMutableArray

2 个解决方案

#1


10  

Since swift types like Tuple or Struct have no equivalent in Objective-C they can not be cast to or referenced as AnyObject which NSArray and NSMutableArray constrain their element types to.

由于像Tuple或Struct这样的swift类型在Objective-C中没有等价物,所以它们不能被强制转换为NSArray和NSMutableArray约束其元素类型的AnyObject。

The next best thing if you must return an NSMutableArray from a swift Array of tuples might be returning an Array of 2 element Arrays:

如果必须从快速的元组数组返回NSMutableArray,那么下一个最好的事情可能是返回一个包含2个元素数组的数组:

let itemsTuple = [("Pheonix Down", "Potion"), ("Elixer", "Turbo Ether")]
let itemsArray = itemsTuple.map { [$0.0, $0.1] }
let mutableItems = NSMutableArray(array: itemsArray)

#2


2  

There are two problems with what you are trying to do:

你要做的事情有两个问题:

  • Swift array can be cast to NSArray, but it cannot be cast to NSMutableArray without constructing a copy
  • Swift数组可以转换为NSArray,但是如果不构造副本,则无法将其强制转换为NSMutableArray

  • Swift tuples have no Cocoa counterpart, so you cannot cast them or Swift collections containing them to Cocoa types.
  • Swift元组没有Cocoa对应物,所以你不能将它们或包含它们的Swift集合转换为Cocoa类型。

Here is how you construct NSMutableArray from a Swift array of String objects:

以下是如何从Swift的String对象数组构造NSMutableArray:

var arr = ["a"]
arr.append("b")
let mutable = (arr as AnyObject as! NSArray).mutableCopy()
mutable.addObject("c")
print(mutable)

#1


10  

Since swift types like Tuple or Struct have no equivalent in Objective-C they can not be cast to or referenced as AnyObject which NSArray and NSMutableArray constrain their element types to.

由于像Tuple或Struct这样的swift类型在Objective-C中没有等价物,所以它们不能被强制转换为NSArray和NSMutableArray约束其元素类型的AnyObject。

The next best thing if you must return an NSMutableArray from a swift Array of tuples might be returning an Array of 2 element Arrays:

如果必须从快速的元组数组返回NSMutableArray,那么下一个最好的事情可能是返回一个包含2个元素数组的数组:

let itemsTuple = [("Pheonix Down", "Potion"), ("Elixer", "Turbo Ether")]
let itemsArray = itemsTuple.map { [$0.0, $0.1] }
let mutableItems = NSMutableArray(array: itemsArray)

#2


2  

There are two problems with what you are trying to do:

你要做的事情有两个问题:

  • Swift array can be cast to NSArray, but it cannot be cast to NSMutableArray without constructing a copy
  • Swift数组可以转换为NSArray,但是如果不构造副本,则无法将其强制转换为NSMutableArray

  • Swift tuples have no Cocoa counterpart, so you cannot cast them or Swift collections containing them to Cocoa types.
  • Swift元组没有Cocoa对应物,所以你不能将它们或包含它们的Swift集合转换为Cocoa类型。

Here is how you construct NSMutableArray from a Swift array of String objects:

以下是如何从Swift的String对象数组构造NSMutableArray:

var arr = ["a"]
arr.append("b")
let mutable = (arr as AnyObject as! NSArray).mutableCopy()
mutable.addObject("c")
print(mutable)