Search code examples
iosswiftnsbundle

Location to save initial data file?


Warning, I'm a designer, stumbling my way around Swift, so apologies ahead of time for my ignorance.

The app I'm working on will ship with a JSON data file with all data (so that the app is accessible without connection). When the app runs I check a remote JSON file to see if there have been any updates to the data. If there is new data then I'll overwrite the local file with the newer JSON file.

My question isn't directly related to code, but the iOS file system. I know that I can't write to the main bundle, so where do I put the starting JSON file so that I can access it from the Documents directory? I understand how to save new files to Documents with code and all that, but I don't know how to put the JSON file into the Documents directory to start with.

Just a point in the right direction would be great.

Thanks


Solution

  • This might be helpful to you. By using this you can create files in the application data or fetch files from application data.

       var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
       var dirPath = paths.stringByAppendingPathComponent("XYZ/")
    
       var docPath = paths.stringByAppendingPathComponent("XYZ/xyz.json" )
       println(docPath)
       var checkImage = NSFileManager.defaultManager()
    
       if (checkImage.fileExistsAtPath(docPath)) {
    
    
        } else
        {
             checkImage.createDirectoryAtPath(dirPath, withIntermediateDirectories: true, attributes: nil, error: nil)
    
        }
    

    For deleting specific use this

       checkImage.removeItemAtPath(imagePath, error: nil)