A simple script for moving a file vom the documentdirectory to a subdirectory don't work
Moved failed with error: The operation couldn’t be completed. (Cocoa error 4.)
var fileManager : NSFileManager = NSFileManager.defaultManager()
var folderDocuments = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
func moveFile(fileDirSource: String, fileDirDestination: String, fileName: String) -> Bool
{
var error: NSError?
var filePathSource : String = folderDocuments
if(fileDirSource != "")
{
filePathSource = folderDocuments.stringByAppendingPathComponent(fileDirSource)
}
filePathSource = filePathSource.stringByAppendingPathComponent(fileName)
var filePathDestination : String = folderDocuments.stringByAppendingPathComponent(fileDirDestination)
filePathDestination = filePathDestination.stringByAppendingPathComponent(fileName)
println(filePathSource)
println(filePathDestination)
if self.checkIfFileExists(filePathSource)
{
if fileManager.moveItemAtPath(filePathSource, toPath: filePathDestination, error: &error)
{
println("Move successful")
return true
}
else
{
println("Moved failed with error: \(error!.localizedDescription)")
return false
}
}
else
{
return false
}
}
I try it with the Simulator of Xcode. The following function returns true, because the file exist in the Documentdirectory. Also the destinationfolder exist and the destinationfolder is empty.
func checkIfFileExists(fileNameWithPath: String) -> Bool
{
return fileManager.fileExistsAtPath(fileNameWithPath)
}
Here are the two pathvalues
SourcePath
/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/5C01BB3D-586E-409D-9378-BEFFE91A410B/Documents/done_123.txt
DestinationPath
/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/5C01BB3D-586E-409D-9378-BEFFE91A410B/Documents/Input/done_123.txt
Here is the function of the AppDelegate, where i start to move the file:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
let controller = masterNavigationController.topViewController as! MasterViewController
controller.managedObjectContext = self.managedObjectContext
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
println("App Path: \(dirPaths)")
// Folder for the files would be created if not exist
var fileshandling = Filehandling()
var arrFoldername = ["Inbox","Outbox","Files"]
for foldername in arrFoldername
{
if(!fileshandling.checkIfFolderExist(foldername))
{
fileshandling.mkdir(foldername)
}
else
{
println("The Folder " + foldername + " exists")
}
}
fileshandling.moveFile("", fileDirDestination: "Input", fileName: "done_123.txt")
println("UUID string: \(uuid)")
return true
}
This is the console in Xcode, when i start to run my app
App Path: > [/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA/Documents]
The Folder Inbox exists
The Folder Outbox exists
The Folder Files exists
/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA/Documents/done_123.txt
/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA/Documents/Input/done_123.txt
Moved failed with error: The operation couldn’t be completed. (Cocoa error 4.)
UUID string: 027E0494-3E24-45B8-A2AC-7E3501BED78A
Here is the screenshot from the terminal of the documentfolder

Inputdirectory does not exist - Matthias Bauch