Unable to copy file from directory to document directory in iOS

I am trying to copy a file from my package to the document directory in iOS with the following code.

let bundlePath = NSBundle.mainBundle().pathForResource("information", ofType: ".png") print(bundlePath, "\n") //prints the correct path let destPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first! let fileManager = NSFileManager.defaultManager() let fullDestPath = NSURL(fileURLWithPath: destPath).URLByAppendingPathComponent("information.png") let fullDestPathString = String(fullDestPath) print(fileManager.fileExistsAtPath(bundlePath!)) // prints true do{ try fileManager.copyItemAtPath(bundlePath!, toPath: fullDestPathString) }catch{ print("\n") print(error) } 

Domain Error = NSCocoaErrorDomain Code = 4 "File" information.png "does not exist." UserInfo = {NSSourceFilePathErrorKey = / Users / macbookpro / Library / Developer / CoreSimulator / Devices / E58CA1C6-C6F1-4D72-9572-3925675E78A5 / data / Containers / Package / Application / EFA83E02-5F24-4BB3-B32A-7E75appay / AutoZoutoutoutouttimeout /information.png, NSUserStringVariant = (copy), NSDestinationFilePath = file: /// Users / macbookpro / Library / Developer / CoreSimulator / Devices / E58CA1C6-C6F1-4D72-9572-3925675E78A5 / data / Containers / Data / Application / 86A1BDD5 FAF2-486E-85A9-CF72A547C6CD / Documents / information.png, NSFilePath = / Users / macbookpro / Library / Developer / CoreSimulator / Devices / E58CA1C6-C6F1-4D72-9572-3925675E78A5 / data / Containers / Package / Application / EFA83E02-5 -4BB3-B32A-7E755081A730 / AutoLayout tuts.app/information.png, NSUnderlyingError = 0x7fb53251cd80 {Domain Error = NSPOSIXErrorDomain Code = 2 " is such file or directory "}}

According to fileManager.fileExistsAtPath() file really exists. What am I doing wrong?

+5
source share
4 answers

The problem is this line:

 let fullDestPathString = String(fullDestPath) 

It should be:

 let fullDestPathString = fullDestPath.path 

Look at the error. The problem is the appointment. Pay attention to file:/// . Your code incorrectly converts the URL to the file path. You need to use the path property for NSURL to get the path as a string.

In all of your debugging and validation, you never checked the value of fullDestPathString .

+10
source

Please find the code below.i by taking the link from @rmaddy's answer.

 func CheckDataBaseOnPathorNot() -> Void { let bundlePath = Bundle.main.path(forResource: "Project_Expert", ofType: ".db") print(bundlePath ?? "", "\n") //prints the correct path let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let fileManager = FileManager.default let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("Project_Expert.db") let fullDestPathString = fullDestPath!.path print(fileManager.fileExists(atPath: bundlePath!)) // prints true if fileManager.fileExists(atPath: fullDestPathString) { print("File is available") }else{ do{ try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPathString) }catch{ print("\n") print(error) } } } 

Check this code if the file is not available in the path, and then copy the file.

Thanks.

+2
source

Swift 3

 func copyfileToDocs() { let bundlePath = Bundle.main.path(forResource: "db", ofType: ".sqlite") print(bundlePath!, "\n") //prints the correct path let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let fileManager = FileManager.default let fullDestPath = NSURL(fileURLWithPath: destPath).appendingPathComponent("db.sqlite") let fullDestPathString = fullDestPath?.path print(fileManager.fileExists(atPath: bundlePath!)) // prints true do { try fileManager.copyItem(atPath: bundlePath!, toPath: fullDestPathString!) print("DB Copied") } catch { print("\n") print(error) } } 
+1
source

to get the string path you have to use this

 let path = fullDestPath.path 

On a side note

 fileManager.fileExistsAtPath(bundlePath!) == true 

is a way to check if true is true

 fileManager.fileExistsAtPath(bundlePath!) 

only this can have some problems

0
source

Source: https://habr.com/ru/post/1238582/


All Articles