如何将快速元组数组转换为NSMutableArray? [英] How to cast a swift array of tuples to NSMutableArray?
问题描述
我有快速元组 [(String,String)]
的数组,并希望将此数组转换为NSMutableArray。我已经尝试过了,但是它不起作用:
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:
let myNSMUtableArray = swiftArrayOfTuples as! AnyObject as! NSMutableArray
推荐答案
由于类型迅速,例如元组
或 Struct
在Objective-C中没有等效项,因此无法将其强制转换为或引用为 AnyObject
其中 NSArray
和 NSMutableArray
将其元素类型限制为。
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.
如果必须从快速元组数组中返回NSMutableArray,则下一个最好的事情可能是返回2个元素数组的数组:
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:
let itemsTuple = [("Pheonix Down", "Potion"), ("Elixer", "Turbo Ether")]
let itemsArray = itemsTuple.map { [$0.0, $0.1] }
let mutableItems = NSMutableArray(array: itemsArray)
这篇关于如何将快速元组数组转换为NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!