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
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.
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)
There are two problems with what you are trying to do:
NSArray
, but it cannot be cast to NSMutableArray
without constructing a copyHere is how you construct NSMutableArray
from a Swift array of String
objects:
var arr = ["a"]
arr.append("b")
let mutable = (arr as AnyObject as! NSArray).mutableCopy()
mutable.addObject("c")
print(mutable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With