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

标签 ios xcode swift

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

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

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

最佳答案

Cocoa error 4 这里意味着你试图移动一个不存在的文件或者这也可能意味着目标目录不存在。

关于ios - 将文件从主文件夹移动到子文件夹失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28873359/

相关文章:

ios - UITabBar 尺寸在 iPhone X 上增加

iOS swift : Using delegate methods in custom class

ios - 修复具有大量状态的巨型 View Controller

ios - 在 Swift 3 和 iOS 中管理 Sqlite 数据库文件(从本地和/或 bundle 复制到应用程序)

xcode - 如何从命令行更新 Xcode

ios - 如何通过 Swift 在拍摄的照片上插入文字?

ios - 如何在后台线程(Swift)上有效地将大文件写入磁盘

ios - 如何添加按钮的触摸区域?

ios - 缩放 CAShapeLayer 时的意外行为

ios - 如何在每次按下 UIButton 时创建一个新的 UILabel?