Why does it not exist via @AppStorage

2 versions 1 works 1 doesn't. UIViewRepresentable to show a PDF

@AppStorage(DefaultsKey.userActiveBook.rawValue) var activeBook : URL = Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")!

func makeUIView(context: Context) -> PDFView {
do {
  let pdfData = try Data(contentsOf: activeBook)
            pdfView.document = PDFDocument(data: pdfData) //<---- is nil
            ...
}
 catch let Error {
       print(Error)
        }


}

fails with

Error Domain=NSCocoaErrorDomain Code=260 "The file “BookPlaceHolder.pdf” couldn’t be opened because there is no such file."

func makeUIView(context: Context) -> PDFView {
do {
  let pdfData = try Data(contentsOf: Bundle.main.url(forResource: "BookPlaceHolder", withExtension: "pdf")!)
            pdfView.document = PDFDocument(data: pdfData)
            ...
}
 catch let Error {
       print(Error)
        }


}

Works perfectly. What is it with using @AppStorage to access the exact same URL causing the discrepancies ?

  • @AppStorage or userdefaults is for storing small bits of data not huge binary blobs.

Add a Comment

Accepted Reply

Solved. Totally forgot that URLs for file system change often so storing the full URL is pointless.

Replies

Solved. Totally forgot that URLs for file system change often so storing the full URL is pointless.