简体   繁体   中英

Copying files from bundle to Libraries folder (IOS10, Swift3, xcode 8.2)

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*

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")
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM