|
My post from 2 days ago hasn't generated any conversation/answers so I decided to go back and take another look at using DocumentGroup to open and save my custom document type even though I don't think it will give me the control I need - i.e. my custom document type contains a stringified path URL to a media (audio) file that I also need to open. Imagine an audio player that lets the user select a portion of the audio and loop it a specified number of times and a specified rate (slow down or speed up). I intend to save these parameters in a JSON file along with the URL to the associated media file. My post the other day describes the problem I have with using custom file Open and Save buttons on the File menu when using a regular WindowGroup and .fileImporter and .fileExporter on buttons in the ContentView hierarchy - but I can't figure out how to access the document from the file menu (either I crash or have compiler errors). So until I have have some feedback on that (or I find enlightenment in my searches) I am once again attempting to use a DocumentGroup. Now I'm running into a different problem but if I can solve this problem, I might be able to use DocumentGroup to accomplish what I need. Here is a stripped down SwiftUI DocumentGroup app that shows the issue:
|
|
I found a post here referring to Day 26 from the course that discusses this in the context of Views. An explicit use of return to return the view but my issue is within a ViewBuilder so adding return generates this error:
Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder' |
|
Check How to create a document-based app using FileDocument and DocumentGroup, hopefully give you some pointers. |
|
Thanks @NigelGee - that is actually one of the source I used when I started this. Unfortunately, it doesn't shed any light on this issue or what I would like to do. There is very little info / design patterns on how to do more complex file management in SwiftUI it seems. This stuff was all easy using the old NSOpenPanel stuff. But I really want this to be as native SwiftUI as possible. I'm very close with my other implementation - except that I can't figure out how to implement File menu Open and Save menu buttons. If I ignore the File menu (remove these option) I would be fine but that's not a friendly Mac app! |
|
A very simple conceptual exercise that would give me the tools and understanding I need is: Imagine you have a text file that has a simple string in it that you want to display at the top of the ContenView and that you would like to allow the user to edit this string and have it persist back to the file. I think the understanding needed to implment this with DocumentGroup would put me on the right track. Perhaps the answer is "it can't be done with the current version of SwiftUI"! thanks, Michael |
|
Can you just add another property to your
|
|
@roosterboy Yes! that works for this simple case so that might give me the building blocks I need. Thank you. So, is it a general design principle that any calculations, parameter settings, etc that a View needs can be performed in the onAppear action? thanks, Michael |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.