How to convert NSSet to [String] array?

noobprogrammer picture noobprogrammer · Jul 27, 2015 · Viewed 12k times · Source

I have an NSSet of Strings, and I want to convert it into [String]. How do I do that?

Answer

Eric Aya picture Eric Aya · Jul 27, 2015

I would use map:

let nss = NSSet(array: ["a", "b", "a", "c"])

let arr = nss.map({ String($0) })  // Swift 2

let arr = map(nss, { "\($0)" })  // Swift 1

Swift 2

Swift 1