Rob
Rob

Reputation: 4234

Ambiguous use of 'filter' when converting project to Swift 4

I tried converting my project to Swift 4 this day. I'm having an error on this line:

return Forum.threads?.filter({ //... })

The error says:

Ambiguous use of 'filter'

Found this candidate (Swift.Set)

Found this candidate (Swift.Sequence)

threads object is implemented like this in Forum:

var threads: Set<Thread>?

So how to solve this..? Thanks for your help

EDIT : when revealing the error in log, here are the candidates:

Swift.Set:369:17: note: found this candidate
    public func filter(_ isIncluded: (Set.Element) throws -> Bool) rethrows -> Set<Element>
                ^
Swift.Sequence:35:17: note: found this candidate
    public func filter(_ isIncluded: (Self.Element) throws -> Bool) rethrows -> [Self.Element]

Upvotes: 13

Views: 4515

Answers (2)

marcospolanco
marcospolanco

Reputation: 156

To solve this, declare the type of the variable before you return it.

let x: [Character] = input.filter{/**/}
return x

This disambiguates the return type of the filter{} method.

Upvotes: 14

James
James

Reputation: 2270

There seems to be a general issue here. For example this is an extension method on UITextView.

ambiguous filter

You can work round it by rewriting as a for loop. (Sorry, not great solution but will at least work.)

Upvotes: 0

Related Questions