Reduce array to set in Swift

Banana picture Banana · Dec 8, 2015 · Viewed 56.5k times · Source

I am trying to reduce an array of objects to a set in Swift and this is my code:

objects.reduce(Set<String>()) { $0.insert($1.URL) }

However, I get an error:

Type of expression is ambiguous without more context.

I do not understand what the problem is, since the type of URL is definitely String. Any ideas?

Answer

NRitH picture NRitH · Dec 8, 2015

You don't have to reduce an array to get it into a set; just create the set with an array: let objectSet = Set(objects.map { $0.URL }).