【问题标题】:Move file from mainfolder to subfolder failed将文件从主文件夹移动到子文件夹失败
【发布时间】:2015-05-06 13:22:19
【问题描述】:

将文件从文档目录移动到子目录的简单脚本不起作用

移动失败并出现错误:操作无法完成。 (可可错误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)
    
}

这是两个路径值

源路径

/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 中的控制台,当我开始运行我的应用程序时

应用路径:> [/Users/rwollenschlaeger/Library/Developer/CoreSimulator/Devices/111A9376-75A8-45CD-86DA-BDA53D8B4EDB/data/Containers/Data/Application/CE93DA6D-8125-48B8-B4EB-C9B706A3A1DA/Documents ]

文件夹收件箱存在

文件夹发件箱存在

文件夹文件存在

/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

移动失败并出现错误:操作无法完成。 (可可错误4。)

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

这是来自文件夹终端的屏幕截图

【问题讨论】:

  • 有根据的猜测:Input 目录不存在
  • 见鬼。该文件夹是收件箱而不是输入。我的天啊。谢谢大家的时间

标签: ios xcode swift


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多