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

时间:2017-09-21 08:58:23

标签: swift

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

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

错误说:

  

模糊地使用'过滤器'

     

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

     

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

threads对象在Forum

中实现如下
var threads: Set<Thread>?

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

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

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]

2 个答案:

答案 0 :(得分:12)

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

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

这消除了过滤器{}方法的返回类型的歧义。

答案 1 :(得分:0)

这里似乎有一个普遍的问题。例如,这是UITextView上的扩展方法。

ambiguous filter

你可以通过重写为for循环来解决它。 (对不起,不是很好的解决方案,但至少会起作用。)