gpt4 book ai didi

swift - 将 Swift Set 转换为 NSMutableSet

转载 作者:搜寻专家 更新时间:2023-10-31 19:27:31 24 4
gpt4 key购买 nike

我可以从 NSMutableSet 转换为 Set 没问题,但在执行相反操作时我遇到了问题。

例如这有效:

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

但是当我尝试时:

let nsSet2 = swiftSet as? NSMutableSet

nsSet2 最终为 nil

最佳答案

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

let nsSet2 = NSMutableSet(set: set as NSSet)

或速记:

let nsSet2 = NSMutableSet(set: set)

或者从 NSSet 到 Swift Set 再回到 NSSet:

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

关于swift - 将 Swift Set 转换为 NSMutableSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49017356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com