将Swift Set转换为NSMutableSet

时间:2018-02-27 20:26:42

标签: swift type-conversion set nsset

我可以从NSMutableSet转换为Set没有问题,但是我在反向时遇到了问题。

E.g。这有效:

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

但是当我尝试时:

let nsSet2 = swiftSet as? NSMutableSet

nsSet2最终成为nil

1 个答案:

答案 0 :(得分:3)

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

<Header>
    <MessageHeader>
        <purpose>
            <unit>Z01</unit>            
        </purpose>
    </MessageHeader>    
</Header>

或简写:

let nsSet2 = NSMutableSet(set: set as NSSet)

或者从NSSet转到Swift Set并返回NSSet:

let nsSet2 = NSMutableSet(set: set)