Cannot load the saved JSON file in a document based App

In a document based app I have created, I have a View called “TestDraw” where I can drag and drop UILables from the collectionView. On the “TestDraw”, free-drawing is also allowed. I have also set BarItems for saving the position where the user put the UILabel and the information on it so that that information can later be decoded into the card object. However, every time I open a JSON file that I think have been saved successfully, it turns out to be blank.

I have tried to print the jsonString of the saved file into the console which proves to be correct and hence shows the information is saved successfully.

Here is the code in the NumberCardsSetDocument:

class NumberCardsSetDocument: UIDocument {

var numberCardsSet: NumberCardsSet?

override func contents(forType typeName: String) throws -> Any {
    // Encode your document with an instance of NSData or NSFileWrapper
    return numberCardsSet?.json ?? Data()

override func load(fromContents contents: Any, ofType typeName: String?) throws {
    if let json = contents as? Data{
        numberCardsSet = NumberCardsSet(json: json)
    }else {
        print("Error trying to load NumberCards. Content is not JSON data")

Here is the code in the NumberCardsSet:

struct NumberCardsSet: Codable{
var numberCards = [CardInfo]()
struct CardInfo: Codable{
    let x: Int
    let y: Int
    let text: String
    let size: Int
    if let newValue = try? JSONDecoder().decode(NumberCardsSet.self, from: json){
        self = newValue
        return nil
var json: Data? {
    return try? JSONEncoder().encode(self)
init (numberCards:[CardInfo]){
    self.numberCards = numberCards

And this is what I am trying to do to load the saved data:

class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate {

override func viewDidLoad() {
    delegate = self
    allowsPickingMultipleItems = false
    allowsDocumentCreation = true
    template = try? FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true).appendingPathComponent("UntitledHMS.json")
    if template != nil {
        allowsDocumentCreation = FileManager.default.createFile(atPath: template!.path, contents: Data() )

var template:URL?
// MARK: UIDocumentBrowserViewControllerDelegate

func documentBrowser(_ controller: UIDocumentBrowserViewController, didRequestDocumentCreationWithHandler importHandler: @escaping (URL?, UIDocumentBrowserViewController.ImportMode) -> Void) {

func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentsAt documentURLs: [URL]) {
    guard let sourceURL = documentURLs.first else { return }
    presentDocument(at: sourceURL)

func documentBrowser(_ controller: UIDocumentBrowserViewController, didImportDocumentAt sourceURL: URL, toDestinationURL destinationURL: URL) {
    presentDocument(at: destinationURL)

func documentBrowser(_ controller: UIDocumentBrowserViewController, failedToImportDocumentAt documentURL: URL, error: Error?) {

// MARK: Document Presentation
func presentDocument(at documentURL: URL) {
    let storyBoard = UIStoryboard(name: "Main", bundle: nil)
    let documentVC = storyBoard.instantiateViewController(withIdentifier: "DocumentMVC")
    if let trailMakingTestViewController = documentVC.contents as? DocumentViewController{
        trailMakingTestViewController.document = NumberCardsSetDocument(fileURL: documentURL)
    present(documentVC,animated: true)

I think the problem probably lies in the process of decoding the information. Hoping someone can give me some clue. This is the first time I tried to build a complicated app on my own.

@feanor Do you still have issues with this?

No, thanks, I fixed it by myself

This topic was automatically closed after 166 days. New replies are no longer allowed.