SwiftUI modifier order matters

This Sunday I spent pretty much all day working on MarsBased.

I woke up early, went to the gym, made myself some coffee and got to work. 

Screen Shot 2021 10 18 at 8 45 12 PM

In front of me was a prioritized list of “feature enhancements” my wife had prepared for me -she’s an excellent Product Manager so she always has great feedback- after testing the app a day early. 

I started going through the list… First couple of suggestions were icon changes which I dispatched in about 5m. Off to a great start. 

Now I reach what is supposed to be an extremely easy one: add contextMenu to the TODO tasks. In essence, for each task you should be able to right click and you’d see a couple options: delete, focus, complete… Easy right? Right?

Well, should have been. Turns out when I added the contextMenu, selection stopped working… You could right click, and would see the options, but normal selection would not work anymore.

Screen Shot 2021 10 18 at 8 51 10 PM

Then I realized -after much, much, much searching and try and error-, that selection would work again if instead of using a Int as the tag I would use an Int64. Why you ask? Excellent question, absolutely no idea.

Solved right? Well.. No

Now the challenge was @SceneStorage would not work with Int64. I could make it work with @State mind you, but not with @SceneStorage… And most importantly, I did not know what was going on which made this approach -even if it were to work (which it did not)- a no go. 

At this point I had been at least four hours working on this and there was no solution in sight…

In the end, I decided not to waste more time. I was stuck, and have enough experience to know (usually) when to stop. 

Went for a walk, relaxed in the garden and went to sleep. Today, after work I decided to take another crack at it. This time I tried a different approach.

I created a new SwiftUI file and started from scratch, testing different combinations of ForEach or Lists. And what do you know, it worked on the first try… Wait what? I compared the code, it was essentially the same, what’s was going on there… 


If you use a tag modifier before the contextMenu, selection will stop working. If you first have the contextMenu modifier and then the tag modifier it works great… Good to know. 

So that’s it, about 6 hours of my life debugging something that turned out to be the order of modifiers. 

Learn from my misakes, modifier order matters 🙂 

Until next time,