Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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]
like image 608
Rob Avatar asked Sep 21 '17 08:09

Rob


1 Answers

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.

like image 136
marcospolanco Avatar answered Nov 14 '22 18:11

marcospolanco