1

Here is my code for copying the folder from bundle to libraries.

    let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
    let bundle = Bundle.main.path(forResource: "MYFOLDER", ofType: "")!

    let fileManager = FileManager.default
    do
    {
        try fileManager.copyItem(atPath: bundle, toPath: library.path)
        print("NO CATCH")
    }
    catch
    {
            print(error)
    }

I get this error:

Error Domain=NSCocoaErrorDomain Code=516 "“MYFOLDER” couldn’t be copied to “CBE6D212-8827-423B-AEC8-6830EED0EC5C” because an item with the same name already exists." UserInfo={NSSourceFilePathErrorKey=/Users/Joshua/Library/Developer/CoreSimulator/Devices/84EF61E3-6739-47D1-A0AE-1215B89D0CE7/data/Containers/Bundle/Application/C357EB0E-6719-464B-8DC9-513E5E7F83E1/Heffy.app/MYFOLDER, NSUserStringVariant=( Copy ), NSFilePath=/Users/Joshua/Library/Developer/CoreSimulator/Devices/84EF61E3-6739-47D1-A0AE-1215B89D0CE7/data/Containers/Bundle/Application/C357EB0E-6719-464B-8DC9-513E5E7F83E1/Heffy.app/MYFOLDER, NSDestinationFilePath=/Users/Joshua/Library/Developer/CoreSimulator/Devices/84EF61E3-6739-47D1-A0AE-1215B89D0CE7/data/Containers/Data/Application/CBE6D212-8827-423B-AEC8-6830EED0EC5C/Library, NSUnderlyingError=0x797878e0 {Error Domain=NSPOSIXErrorDomain Code=17 "File exists"}}

**BUT I CHECKED IT DOES NOT EXIST IN THE LIBRARY FOLDER*

blake
  • 11
  • 7
  • Delete drive data, delete project from your simulator and clean your x-code project , build and run again. Sometimes it might be happened. Everything is fine but still got error. If you still got error let me know. – dahiya_boy Dec 17 '16 at 08:24
  • @TinuDahiya Sorry for asking this question but how to i delete the drive data and the project from the simulator? – blake Dec 17 '16 at 08:47
  • @TinuDahiya I just did Simulator->reset Content and Settings. Simulator is now loading. I'll report what happens next. – blake Dec 17 '16 at 08:50
  • open your finder and in the left side bar there you see list,-> application , Downloads, Documents, and Drive Data . Drive data have all the data of projects you run in your xcode. Just like app data when you install a new app in your phone – dahiya_boy Dec 17 '16 at 08:54
  • @TinuDahiya I still get the error. – blake Dec 17 '16 at 09:03
  • [More details for drive data](http://stackoverflow.com/questions/18933321/can-i-safely-delete-contents-of-xcode-derived-data-folder) – dahiya_boy Dec 17 '16 at 09:04
  • Same problem like you have but Code is in swift 2, convert into 3.0 http://stackoverflow.com/questions/34346220/cant-copy-file-from-bundle-to-documents-directory-in-ios – dahiya_boy Dec 17 '16 at 09:07
  • 1
    @TinuDahiya "derived data", not "drive data"! :) – Eric Aya Dec 17 '16 at 09:10

2 Answers2

0

As per your error, file is already existing. try this code and check

  let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
let filePath = url.appendingPathComponent("NewFolder")?.path
let fileManager = FileManager.default
if fileManager.fileExists(atPath: filePath!) {
    print("FILE AVAILABLE")
} else {
    print("FILE NOT AVAILABLE")
}
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

So I got pissed so I closed Xcode. Closed my browser. Turn my machine off and turned it on. Guess what not it works........ Thanks for the help TinuDahiya I appreciate it ^__^V.

blake
  • 11
  • 7