Sonoma 14.5, Xcode 16.0 (16A242d)
Pure SwiftUI application, MacOS (document-based), a single TextView with a binding instead of the template solution with added UndoManager functionality (which seems unnecessary).
The error does not happen every time:
– if I type in an existing document, it's fine; if I switch to Xcode and back to my app, the error appears.
– if I create a new document and select the existing string, it's fine. If I create a second new document and select the string in that, I get the error.
– if I switch between two dirty unsaved documents, I get the error.
and so on. I'm reasonably confident there are patterns to this, but I don't have the time or patience to try all of them.
Post
Replies
Boosts
Views
Activity
This was driving me mad as well, because half the examples (including in this state answer 'how do I preview a @Binding' with 'here's a working example using @State' which misses the point by a mile. I've found different workarounds elsewhere that also didn't work for me.
What does work, however, is to replace @Binding with @Bindable. And suddenly, the standard
#Preview macro works out of the box.
#Preview {
BindingViewExample_2(value: true)
}
I had this problem with trying to create an NSImage from a URL retrieved from an openDialog (well, a SwiftUI .fileImporter) even after setting the UserSelectedFile Sandbox permission. (If it's 'none', it'll crash, so it's hard to get that one wrong).
The fileImporter retrieves a valid URL, but cannot access the file anyway. (This used to work).
The answer is to make the resource explicitly accessible:
_ = selectedFileURL.startAccessingSecurityScopedResource()
guard let image = NSImage(contentsOf: selectedFileURL) else { return } selectedFileURL.stopAccessingSecurityScopedResource()
In Xcode 16b the first error is slightly rephrased:
BOOL _NSPersistentUIDeleteItemAtFileURL(NSURL *const __strong) Failed to stat item: file:///Users/username/Library/Containers/appIdentifier/Data/Library/Saved%20Application%20State/appIdentifier.savedState/restorecount.plist
and every app, including Apple examples has it.