Reputation: 151
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
}
init?(json:Data){
if let newValue = try? JSONDecoder().decode(NumberCardsSet.self, from: json){
self = newValue
}else{
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() {
super.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) {
importHandler(template,.copy)
}
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.
Upvotes: 1
Views: 158
Reputation: 150655
I think you could narrow this down further. You have this line
return try? JSONEncoder().encode(self)
And you handle nil returns in your code, which is okay.
But if you suspect that your encoding may be failing, one way to get better information is to use a proper do - catch
block around the try
rather than using the try?
. If you start by just printing the error to the console it might help. In my experience, these messages are helpful in pointing out why something cannot be encoded (or decoded).
Upvotes: 1