norlin
norlin

Reputation: 1275

How to use a map method on Set<T>?

Sorry for the stupid question, but I can't find the answer in Swift documentation...

let set: Set<String> = ["foo1", "foo2", "foo3"]
let res: Set<String> = set.map { return $0+"_bar" } // Ambiguous reference to member 'map'

How can I use map method on Set<T>?

p.s. Suggested "duplicate" question: 1) about other issue; 2) answers doesn't work in current Swift version

Upvotes: 0

Views: 77

Answers (2)

David Berry
David Berry

Reputation: 41226

The problem is that set.map (really CollectionType.map) returns an Array, not a Set. Hence:

let res = set.map { $0 + "_bar" }

Works and leaves you with [String] which you can convert back to a Set<String> with the constructor:

let res : Set<String> = Set(set.map{$0 + "_bar"})

Upvotes: 1

norlin
norlin

Reputation: 1275

Ah, the answer is pretty easy – thanks to other community for the help!

let set: Set<String> = ["foo1", "foo2", "foo3"]
let res: Set<String> = Set(set.map { return $0+"_bar" })

Upvotes: 0

Related Questions