saoudrizwan / Disk

Easily persist structs, images, and data on iOS
MIT License
3.1k stars 170 forks source link

Permission denied during read operation #25

Closed y1337x closed 6 years ago

y1337x commented 6 years ago

I've saved an object with try! Disk.save(recipe, to: .documents, as: "Recipes/"+recipe.fileName+".json") and tried to load it with try! Disk.retrieve("Recipes", from: .documents, as: [Recipe].self) and got the following error: Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 "The file “Ingredients” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/danyel/Library/Developer/CoreSimulator/Devices/9D629C11-EC8B-4B0D-A5FD-1D2FC8B1D5F5/data/Containers/Data/Application/A55E48FA-C5C6-4796-8A32-946E51313879/Documents/Ingredients, NSUnderlyingError=0x60400024d650 {Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"}}

Any ideas for a solution?

saoudrizwan commented 6 years ago

You can't retrieve an array of Recipes from multiple JSON files. If you are trying to save an array, then you would save that to one JSON file.

For example:

struct Recipe {
    let name: String
}

let recipes = [Recipe(name: "Sandwich"), Recipe(name: "Steak"), Recipe(name: "Pizza")]

try! Disk.save(recipes, to: .documents, as: "recipes.json")

let retrieved = try! Disk.retrieve("recipes.json", from: .documents, as: [Recipe].self)
y1337x commented 6 years ago

My bad facepalm It's working now! 👍 Thank you very much, I like your library very much! Makes things so easy :)

saoudrizwan commented 6 years ago

@DanyelSuxdorf glad you like it! Keep in mind though that arrays of UIImages or Data types are stored as multiple files in folders (since you can't, for example, have multiple images' data in one PNG file.)