Xcode 9.1 - Ambigous reference to member filter

2019-08-24 02:00发布

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条回答
劳资没心,怎么记你
2楼-- · 2019-08-24 02:39

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.

查看更多
登录 后发表回答