Xcode 9.1 - Ambigous reference to member filter

可紊 提交于 2019-12-25 09:31:49

问题


I already experienced this issue in the past, I fixed it.

But today I just download the new Xcode version 9.1 and my App is not building anymore, I got :

Ambiguous reference to member 'filter'

I don't know why, this is not the piece of code I was working on. The app is building/compiling fine since weeks.

When I check the Release Note on the Official Apple Website, I don't seem to find any reference to my issue.

So here is the piece of code that was working perfectly 2 hours ago :

var severeWeather: Results<SevereWeather>?
var vigiArray = Array<SevereWeather>()
var redCount: Int = 0

severeWeather = realm.objects(SevereWeather.self).filter(NSPredicate(format: "department != nil"))
.sorted(byKeyPath: "departmentNumber")

vigiArray = Array(severeWeather!)

redCount = vigiArray.filter { $0.dangerLevels.filter { $0.level.value == 4 }.count > 0 }.count

What is wrong with my code ?


回答1:


RealmCollection also has a filter method, which is implemented differently. For some reason, Swift compiler doesn't know which one it should refer.

What about this:

redCount = vigiArray.filter {
    return $0.dangerLevels.filter(NSPredicate(format: "%K == %@", "level.value", NSNumber(integerLiteral: 4))).count > 0
}.count

Instead of accessing each element in dangerLevels yourself, build Predicate and let realm do the job for you.

Replace your nest filter with this and it should be compiling just fine.



来源:https://stackoverflow.com/questions/47308773/xcode-9-1-ambigous-reference-to-member-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!