Recently, I noticed that the newly introduced fileImporter
and fileExporter
view modifiers in SwiftUI won’t work inside a Menu
view. It’s not a big deal to put the file modifiers outside of the menu, but it took quite a while for me to pin down the problem.
SwiftUI
SwiftUI and the Problems of Timing
Well, SwiftUI is certainly a big improvement on building maintainable, robust and nice user interfaces on iOS and macOS compared to the old MVC architecture (Massive View Controller) and the NIB (Next Interface Builder). But it is mainly a declarative framework and from the application developer’s point of view it’s sometimes hard to anticipate what is really going on behind the scenes and especially when its going to happen. There are situations where the promised behaviour doesn’t take place and I’ve already struggled hours for hours to look for a workaround.
Especially the .onAppear
modifier seems to be problematic. It looks as if the supplied closure will be executed too early sometimes and the statements inside do not have the desired effect.
Creating a macOS document based app with SwiftUI and the new App protocol
Have you already tried to create a macOS document based app with SwiftUI and the new App
protocol? Well, there are some problems to solve esp. if you want to use menus focused on the currently open document.
In the old app cycle with AppDelegate / SceneDelegate
you still had to use the storyboard to define the menu items and link them to your methods in your document class derived from NSDocument
. Enabling and disabling of the menu item according to the open documents was automatically managed by the frameworks.
That’s not the case in the new App
protocol of SwiftUI anymore. Instead, you can define your menus and menu items quite nicely in your App
struct conforming to the new App
protocol but there is no automatic link to the current document in focus.
SwiftUI Previews: 5s Update Error
Currently, I am developing a quite complex Mac application using Core Data together with SwiftUI. The first challenge I encountered was the SwiftUI preview. I implemented a special preview model with some test data that is assigned to the preview structs of my views as EnvironmentObject. That works fine and I enriched my application step by step.
Well, but suddenly the previews stopped to work. I always got the error message „Updating took more than 5 seconds.
“ I suspected one of the recent changes to screw up my work although the completely built applications still worked fine. I walked through my code and certainly found some problematic points but nothing could make the preview show up again.