jjatie jjatie - 4 years ago 84
iOS Question

How to cast Array<String> to NSArray full of NSString

In working with CloudKit, all types need to be Objective-C foundation classes. Since most types used in CloudKit are bridged from Swift to Obj-C there isn't any issue (i.e.

record[key] = 1 as NSNumber
), but I'm having trouble converting an
to the appropriate CloudKit type (a string list in the CK dashboard). I figured

record[key] = ([myString] as [NSString]) as NSArray

would work, but no such luck. How can I convert this?

Answer Source

Just bridging will work, together with NSArray's array constructor:

let a = ["a", "bc", "def"]
let nsa = NSArray(array: a)
nsa[0] is NSString // *** true ***
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download