斯威夫特设置为数组 [英] Swift Set to Array
本文介绍了斯威夫特设置为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是的NSSet
可转换为阵列
使用 set.allObjects()
但在新的设置
(斯威夫特与1.2引入)没有这样的方法。它仍然可以通过转换斯威夫特设置为的NSSet并使用 allObjects()
方法来实现,但不是最优的。
An NSSet
can be converted to Array
using set.allObjects()
but there is no such method in the new Set
(introduced with Swift 1.2). It can still be done by converting Swift Set to NSSet and use the allObjects()
method but that is not optimal.
推荐答案
您可以创建从一个给定斯威夫特所有元素的数组设置
简单地用
You can create an array with all elements from a given Swift
Set
simply with
let array = Array(someSet)
这工作,因为设置
符合序列类型
协议
和阵列
可以用序列进行初始化。例如:
This works because Set
conforms to the SequenceType
protocol
and an Array
can be initialized with a sequence. Example:
let mySet = Set(["a", "b", "a"]) // Set<String>
let myArray = Array(mySet) // Array<String>
print(myArray) // [b, a]
这篇关于斯威夫特设置为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文