将文件从主文件夹移动到子文件夹失败

时间:2015-03-05 08:39:11

标签: ios xcode swift

将文件目录中的文件移动到子目录的简单脚本

  

已移动失败并显示错误:无法完成操作。 (可可错误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,因为该文件存在于Documentdirectory中。目标文件夹也存在,目标文件夹为空。

func checkIfFileExists(fileNameWithPath: String) -> Bool
{
    return fileManager.fileExistsAtPath(fileNameWithPath)

}

以下是两个路径值

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 < / p>

的DestinationPath

  

/用户/ rwollenschlaeger /库/开发商/ CoreSimulator /设备/ 111A9376-75A8-45CD-86DA-BDA53D8B4EDB /数据/容器/数据/应用/ 5C01BB3D-586E-409D-9378-BEFFE91A410B /文档/输入/ 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中的控制台

  

应用路径:&gt; [/用户/ rwollenschlaeger /库/开发商/ CoreSimulator /设备/ 111A9376-75A8-45CD-86DA-BDA53D8B4EDB /数据/容器/数据/应用/ CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA /文档]

     

文件夹收件箱存在

     

文件夹发件箱存在

     

文件夹文件存在

     

/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA/Documents/done_123.txt < / p>      

/用户/ rwollenschlaeger /库/开发商/ CoreSimulator /设备/ 111A9376-75A8-45CD-86DA-BDA53D8B4EDB /数据/容器/数据/应用/ CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA /文档/输入/ done_123。 TXT

     

已移动失败并显示错误:无法完成操作。 (可可错误4。)

     

UUID字符串:027E0494-3E24-45B8-A2AC-7E3501BED78A

以下是文件夹终端的屏幕截图 The Terminalscreenshot

1 个答案:

答案 0 :(得分:1)

Cocoa error 4这意味着您正在尝试移动不存在的文件,或者这也可能意味着目标目录不存在。