ReferenceFileDocument or FileDocument with Package types

I would like to use ReferenceFileDocument or FileDocument but the file type I use is a package type that conforms to com.apple.package.

However the FileDocument and ReferenceFileDocument respective write methods are called with a inout FileWrapper.

I need this this FileWrapper to be a directory FileWrapper so that i can add child items to it. However the write method is being called with a regularFile instance of the FileWrapper so i am unable to add nested files that I need within my package.

My writableContentTypes is UTType(exportedAs: "com.example.package", conformingTo: .package). And my Xcode project expires/declares this data type as a package, so that UIDocument/NSDocument work.
Answered by Developer Tools Engineer in 614734022
Hi hishnash! SwiftUI provides you with a default value for the FileWrapper that is suitable for most developers' needs. Because the FileWrapper parameter is inout, you can replace it with a new one that is directory-based when you are writing a package to disk:

Code Block swift
func write(to fileWrapper: inout FileWrapper, contentType: UTType) throws {
let children: [String : FileWrapper] = <# construct your file hierarchy here #>
fileWrapper = FileWrapper(directoryWithFileWrappers: children)
}


If you think there's room to improve this API, I encourage you to let the SwiftUI team know via the Feedback Assistant!
Accepted Answer
Hi hishnash! SwiftUI provides you with a default value for the FileWrapper that is suitable for most developers' needs. Because the FileWrapper parameter is inout, you can replace it with a new one that is directory-based when you are writing a package to disk:

Code Block swift
func write(to fileWrapper: inout FileWrapper, contentType: UTType) throws {
let children: [String : FileWrapper] = <# construct your file hierarchy here #>
fileWrapper = FileWrapper(directoryWithFileWrappers: children)
}


If you think there's room to improve this API, I encourage you to let the SwiftUI team know via the Feedback Assistant!
ReferenceFileDocument or FileDocument with Package types
 
 
Q