如何将 NSSet 转换为 [String] 数组?
How to convert NSSet to [String] array?
我有一个 NSSet 字符串,我想将它转换成 [String]。我该怎么做?
我会使用 map
:
let nss = NSSet(array: ["a", "b", "a", "c"])
let arr = nss.map({ String([=10=]) }) // Swift 2
let arr = map(nss, { "\([=10=])" }) // Swift 1
你可以这样做。
let set = //Whatever your set is
var array: [String] = []
for object in set {
array.append(object as! String)
}
let set = NSSet(array: ["a","b","c"])
let arr = set.allObjects as! [String]
如果你有一个Set<String>
,你可以使用数组构造函数:
let set: Set<String> = // ...
let strings = Array(set)
或者,如果您有 NSSet,则有几个不同的选项:
let set: NSSet = // ...
let strings1 = set.allObjects as? [String] // or as!
let strings2 = Array(set as! Set<String>)
let strings3 = (set as? Set<String>).map(Array.init)
我有一个 NSSet 字符串,我想将它转换成 [String]。我该怎么做?
我会使用 map
:
let nss = NSSet(array: ["a", "b", "a", "c"])
let arr = nss.map({ String([=10=]) }) // Swift 2
let arr = map(nss, { "\([=10=])" }) // Swift 1
你可以这样做。
let set = //Whatever your set is
var array: [String] = []
for object in set {
array.append(object as! String)
}
let set = NSSet(array: ["a","b","c"])
let arr = set.allObjects as! [String]
如果你有一个Set<String>
,你可以使用数组构造函数:
let set: Set<String> = // ...
let strings = Array(set)
或者,如果您有 NSSet,则有几个不同的选项:
let set: NSSet = // ...
let strings1 = set.allObjects as? [String] // or as!
let strings2 = Array(set as! Set<String>)
let strings3 = (set as? Set<String>).map(Array.init)