0

I'm having a very difficult time understanding Mapbox expressions in Swift. For example, I write the following:

let falseExpression: MapboxMaps.Expression = Exp(.literal) { false }
let combinedFilter: MapboxMaps.Expression = Expression(operator: .all, arguments: [
 Expression.Argument.expression(workIdFilter),
 Expression.Argument.expression(falseExpression)
])

Then I apply "combinedFilter" to the filter property of a layer. I would expect this to always evaluate to false, since 'falseExpression' is literally false, and ".all" means that all the expressions have to be true. And that means it should hide every feature on the layer. But, it doesn't do anything. All the features remain visible.

If I apply 'falseExpression' directly to the layer instead of combinedFilter, it does hide the features. Or if I omit 'workIdFilter' from the combinedFilter, it also evaluates to false and hides all of the features. ('workIdFilter' evaluates to true in this case).

I'm either misunderstanding what (.all) means or I'm writing this wrong.

asked Mar 8, 2025 at 17:48

1 Answer 1

0

I found the answer:

In my example I referenced 'workIdFilter' but didn't show its definition because it didn't matter, or shouldn't have mattered. Here it is:

let workIdFilter: MapboxMaps.Expression = Exp(.not) {
 Exp(.inExpression) {
 Exp(.get) { "WorkId" }
 Array(hiddenWorkIds)
 }
}

What I'm trying to do here is hide features where the property "WorkId" is contained in the array "hiddenWorkIds".

And here's the problem - when "hiddenWorkIds" is an empty array, it doesn't work right. I can't tell exactly what is happening but I'm guessing there's some error internally that is causing the whole expression to fail and therefore not hide anything.

The workaround is pretty simple -- I just put some dummy value into the array to make sure it is never empty.

This seems like a bug to me. Surely .inExpression should accept an empty list and behave as expected?

answered Mar 10, 2025 at 17:31
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.