I have a JSON file witch copied locally into xcode which I can read it through the main bundle. But then I have problem update this JSON file because everytime i write into a new JSON file create in app document folder.
Here are the sample JSON file
[
{
"id": "5c8a80f52dfee238898d64cf",
"firstName": "Phoebe",
"lastName": "Monroe",
"email": "phoebemonroe@furnafix.com",
"phone": "(903) 553-3410"
},
{
"id": "5c8a80f575270ddb54a18f86",
"firstName": "Lidia",
"lastName": "Wilkins",
"email": "lidiawilkins@furnafix.com",
"phone": "(997) 482-3866"
}
]
Here are the code which I update the JSON file
let mainUrl = Bundle.main.url(forResource: "data", withExtension: ".json")!
let json = JSON(contact)
let hello = json.arrayObject
let str = hello?.description
let data = str!.data(using: String.Encoding.utf8)!
do{
try data.write(to: mainUrl, options: [])
} catch {
print(error)
}
}