用于将文件从文档目录移动到子目录的简单脚本不起作用
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
}
}
我用 Xcode 的模拟器试了一下。以下函数返回 true,因为该文件存在于 Document 目录中。目标文件夹也存在且目标文件夹为空。
func checkIfFileExists(fileNameWithPath: String) -> Bool
{
return fileManager.fileExistsAtPath(fileNameWithPath)
}
这是两个路径值
源路径
/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/5C01BB3D-586E-409D-9378-BEFFE91A410B/Documents/done_123.txt
目标路径
/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
这是 AppDelegate 的函数,我从这里开始移动文件:
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
}
这是 Xcode 中的控制台,当我开始运行我的应用程序时
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
这是文件夹终端的屏幕截图
最佳答案
Cocoa error 4
这里意味着你试图移动一个不存在的文件或者这也可能意味着目标目录不存在。
关于ios - 将文件从主文件夹移动到子文件夹失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28873359/