J.D. J.D. - 1 month ago 21
Swift Question

Creating a json file from Core Data

I am stuck trying to save y data from core data into a json file.

Can anyone give hints about the best way to do this? Perhaps a tutorial.


EDIT: I'd like to store my data in json file in the app directory. SO that latter I could export it into iCloud for example. I couldn't find much about creating a json file... Strange is that for reading a json file I found plenty....


It seems like it would be a lot of trouble to store files in JSON if they are already persisted in CoreData, especially since if you are using Cloudkit, you will not be uploading the files in JSON format. I think you could accomplish what you want though by first creating a dictionary for your objects:

let messageDic = ["message": "Hello Motto"]

and then serialize the dictionary in JSON:

 do {
        let messageData = try JSONSerialization.data(withJSONObject: messageDic, options: .prettyPrinted)

    } catch {
            print("error with serialization")