将项目转换为Swift 4时模糊使用"过滤器"

Rob*_*Rob 11 swift

我今天尝试将我的项目转换为Swift 4.我在这一行上有错误:

return Forum.threads?.filter({ //... })
Run Code Online (Sandbox Code Playgroud)

错误说:

模糊地使用'过滤器'

找到这个候选人(Swift.Set)

找到这个候选人(Swift.Sequence)

threads对象是这样实现的Forum:

var threads: Set<Thread>?
Run Code Online (Sandbox Code Playgroud)

那么如何解决这个问题呢?谢谢你的帮助

编辑:当在日志中显示错误时,以下是候选人:

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]
Run Code Online (Sandbox Code Playgroud)

小智 12

要解决此问题,请在返回变量之前声明变量的类型.

let x: [Character] = input.filter{/**/}
return x
Run Code Online (Sandbox Code Playgroud)

这消除了filter {}方法的返回类型的歧义.

  • 效果很好.另一种方法是`(input.filter {/**/} as [Character])`如果你不想要一个临时变量. (3认同)