I'm beginning to wonder just how viable SwiftUI is for apps. Keep running into serious issues.
For example, I have several existing UIKit apps. All have the same exact about screen. I created a storyboard with the UI along with a view controller .swift file that can be added to all my projects via simple file reference.
In trying to do the same with SwiftUI, I created an AboutView.swift file that lives outside the project's folder hierarchy:
In my app project, I just dragged in the dependent AboutView.swift file.
Xcode (12.4) refuses to generate a preview though for AboutView. Giving me "Cannot preview in this file - active scheme does not build this file". That is false, when adding AboutView to the project, I included it in the app and UITest targets.
If I copy AboutView into the project (now just another local source file specific to that project), previews work again. I refuse to have twelve copies of these shared files; one per project!
I also tried folder references (e.g. dragging in that Common folder above). Same issue.
Tried a workspace that contained my app project and a simple project with my common views. Same issue.
Didn't even try local packages due to those failing miserably for me (separate thread on this forum with all those issues).
Side note: Building and running the app works great, it shows my about screen.
How is one supposed to be able to create a SwiftUI view and shared it across two or more projects? Why can't these be previewed? Each of my apps Assets.xcassets contains a Logo asset that is used in the about screen, so would be nice to see it in a preview and not have to rely on running the app in the simulator or on a device.
Finally, I'm thinking that Xcode can only generate previews of SwiftUI views in a project IFF their files are direct decendents on the project file's main folder on disk. Is that true? If so, why?
For example, I have several existing UIKit apps. All have the same exact about screen. I created a storyboard with the UI along with a view controller .swift file that can be added to all my projects via simple file reference.
In trying to do the same with SwiftUI, I created an AboutView.swift file that lives outside the project's folder hierarchy:
Code Block + Common + Views * AboutView.swift + MyApp * MyApp.xcodeproj + Views + (folder/file reference here for Common items) ...
In my app project, I just dragged in the dependent AboutView.swift file.
Xcode (12.4) refuses to generate a preview though for AboutView. Giving me "Cannot preview in this file - active scheme does not build this file". That is false, when adding AboutView to the project, I included it in the app and UITest targets.
If I copy AboutView into the project (now just another local source file specific to that project), previews work again. I refuse to have twelve copies of these shared files; one per project!
I also tried folder references (e.g. dragging in that Common folder above). Same issue.
Tried a workspace that contained my app project and a simple project with my common views. Same issue.
Didn't even try local packages due to those failing miserably for me (separate thread on this forum with all those issues).
Side note: Building and running the app works great, it shows my about screen.
How is one supposed to be able to create a SwiftUI view and shared it across two or more projects? Why can't these be previewed? Each of my apps Assets.xcassets contains a Logo asset that is used in the about screen, so would be nice to see it in a preview and not have to rely on running the app in the simulator or on a device.
Finally, I'm thinking that Xcode can only generate previews of SwiftUI views in a project IFF their files are direct decendents on the project file's main folder on disk. Is that true? If so, why?
Solution found: At most one Xcode project can be open that contains your shared SwiftUI views. Read on for final details on this crazy journey
The plot thickens... Now the project where it started to magically work now fails again. I also found no differences in project settings between all my attempted projects.
Also, the original project the houses that shared SwiftUI view... it too now fails with cannot preview in this file... Zapping that project's derived data fixes the issue.
Re-opened separate project that had worked above. Still fails. Zapped its derived data. Preview still fails. Zapped shared project's derived data. Preview works!
Appears that derived data that is messing up everything. NOTE: For my shared views, I had "wrapped them" inside their own project. But this is most likely messing things up since I have a single SwiftUI file I want to share, but the build system cannot deal with that setup.
Then tried the following (also fails). Created a simple folder structure on my data drive (IIKit folder containing a Views subfolder containing IIAboutView.swift). Deleted all derived data. If I open one of my app project, preview works. Once I try to work on another project (even when closing the first), preview dies there. Re-opening the original project, preview dies there too.
My derived data is set to "Unique". Tried "Shared" same issues... but then...
Previews started to work again. Then it finally clicked. As long you only have one Xcode project open at any given time that uses the shared SwiftUI view source, all is well! I can easily now repeat the issue by opening a second Xcode project. Previews now fail with that error. You have to then close all Xcode projects and just open one.
I believe during this morning's vast majority of attempts, I had my IIKit project open.
Not a huge deal to only have a single Xcode project open at a time. At least a workaround exists. Will though still file a bug.
The plot thickens... Now the project where it started to magically work now fails again. I also found no differences in project settings between all my attempted projects.
Also, the original project the houses that shared SwiftUI view... it too now fails with cannot preview in this file... Zapping that project's derived data fixes the issue.
Re-opened separate project that had worked above. Still fails. Zapped its derived data. Preview still fails. Zapped shared project's derived data. Preview works!
Appears that derived data that is messing up everything. NOTE: For my shared views, I had "wrapped them" inside their own project. But this is most likely messing things up since I have a single SwiftUI file I want to share, but the build system cannot deal with that setup.
Then tried the following (also fails). Created a simple folder structure on my data drive (IIKit folder containing a Views subfolder containing IIAboutView.swift). Deleted all derived data. If I open one of my app project, preview works. Once I try to work on another project (even when closing the first), preview dies there. Re-opening the original project, preview dies there too.
My derived data is set to "Unique". Tried "Shared" same issues... but then...
Previews started to work again. Then it finally clicked. As long you only have one Xcode project open at any given time that uses the shared SwiftUI view source, all is well! I can easily now repeat the issue by opening a second Xcode project. Previews now fail with that error. You have to then close all Xcode projects and just open one.
I believe during this morning's vast majority of attempts, I had my IIKit project open.
Not a huge deal to only have a single Xcode project open at a time. At least a workaround exists. Will though still file a bug.