本文介绍了将 Swift 集转换为 NSMutableSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从 NSMutableSet 转换为 Set 没问题,但我在反向操作时遇到了问题.

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

例如这有效:

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

但是当我尝试时:

let nsSet2 = swiftSet as? NSMutableSet

nsSet2 最终成为 nil.

推荐答案

貌似swift Sets需要先转换成NSSet:

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

let nsSet2 = NSMutableSet(set: set as NSSet)

或简写:

let nsSet2 = NSMutableSet(set: set)

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

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)

这篇关于将 Swift 集转换为 NSMutableSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 04:11