After upgrading my project to Swift 2.1, NSFileManger
copyItemAtPath
is throwing the following error.
Error Domain=NSCocoaErrorDomain Code=260 "The file "Settings.plist" couldn’t be opened because there is no such file."
NSFilePath=/Users/nick/Library/Developer/CoreSimulator/Devices/95856F10-9770-44B5-B990-B461E6991B79/data/Containers/Bundle/Application/17AAAD05-33FE-470A-9617-C8E378371E2E/MyApp.app/Settings.plist,
NSDestinationFilePath=/Users/nick/Library/Developer/CoreSimulator/Devices/95856F10-9770-44B5-B990-B461E6991B79/data/Containers/Data/Application/020503D6-F7CE-47D2-83A7-D4B7D86E6D1D/Documents/Settings.plist,
NSUnderlyingError=0x7994daf0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Both the underlying source file and destination folder exist, but the copy always fails.
let _documentsDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let storePath = NSURL(fileURLWithPath: _documentsDirectory).URLByAppendingPathComponent("\(storeName).plist").absoluteString
let fileManager = NSFileManager.defaultManager()
if (!fileManager.fileExistsAtPath(storePath)) {
if let bundle = NSBundle.mainBundle().pathForResource(storeName, ofType:"plist") {
do {
try fileManager.copyItemAtPath(bundle, toPath: storePath)
return storePath
} catch let error as NSError {
print(error)
return nil
}
}...
The file exists and print("Exists:", fileManager.fileExistsAtPath(bundle))
returns true.
After reviewing How to find NSDocumentDirectory in swift? and others, I have tried a number of different methods to obtain the Documents directory and the main bundle resources, but I just can't get the file to copy.
Everything was working perfectly in Swift 1.2.