Convert Swift Set to NSMutableSet

丶灬走出姿态 提交于 2019-12-12 12:16:40

问题


I can convert from NSMutableSet to Set no problem, but I'm running into problems when doing the reverse.

E.g. this works:

let nsSet = NSMutableSet(array: ["a", "b"])
let swiftSet = nsSet as! Set<String>

But when I try:

let nsSet2 = swiftSet as? NSMutableSet

nsSet2 ends up being nil.


回答1:


Looks like swift Sets need to be converted to NSSet first:

let nsSet2 = NSMutableSet(set: set as NSSet)

Or shorthand:

let nsSet2 = NSMutableSet(set: set)

Or to go from NSSet to Swift Set and back to NSSet:

let nsSet = NSMutableSet(array: ["a", "b"])
let set = nsSet as! Set<String>
let nsSet2 = set as NSSet
let nsSet3 = NSMutableSet(set: nsSet2)


来源:https://stackoverflow.com/questions/49017356/convert-swift-set-to-nsmutableset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!