将 Swift 集转换为 NSMutableSet

Convert Swift Set to NSMutableSet

我可以从 NSMutableSet 转换为 Set 没问题,但我 运行 在进行反向操作时遇到问题。

例如这有效:

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

但是当我尝试时:

let nsSet2 = swiftSet as? NSMutableSet

nsSet2 最终成为 nil

看起来像 swift 集需要先转换为 NSSet:

let nsSet2 = NSMutableSet(set: set as NSSet)

或shorthand:

let nsSet2 = NSMutableSet(set: set)

或从 NSSet 转到 Swift 集并返回 NSSet:

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