【问题标题】:NSFileManager fileExistsAtPath unable to overwrite existing file in Swift 2NSFileManager fileExistsAtPath 无法覆盖 Swift 2 中的现有文件
【发布时间】:2015-11-30 23:01:01
【问题描述】:

您好,我正在将现有代码升级到 Swift 2,我需要一些有关 DocumentDirectoy 中文件副本的帮助。

这是我用来检查文件是否存在的转换代码,如果存在,我们应该以任何一种方式复制它,但它一直返回一个错误,说文件存在,这是真的,但我们需要覆盖它。

func copyXMLFile()
    {
    // Get a reference for the Document directory
    let rootPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0] 
    // Get a reference for the data.xml file
    xmlPathInDocument = rootPath.stringByAppendingString("data.xml")
    if NSFileManager.defaultManager().fileExistsAtPath(xmlPathInDocument){
        print("xml file exists")

                let xmlPathInBundle = NSBundle.mainBundle().pathForResource("data", ofType: "xml")!
        do {
            // copy file from main bundle to documents directory
            print("copy")
            try NSFileManager.defaultManager().copyItemAtPath(xmlPathInBundle, toPath: xmlPathInDocument)
        } catch let error as NSError {
            // Catch fires here, with an NSErrro being thrown
            print("error occurred, here are the details:\n \(error)")
        }
    }
    else
    {
        // copy the file either way

        let xmlPathInBundle = NSBundle.mainBundle().pathForResource("data", ofType: "xml")!
        do {
            // copy file from main bundle to documents directory
            print("copy")
            try NSFileManager.defaultManager().copyItemAtPath(xmlPathInBundle, toPath: xmlPathInDocument)
        } catch let error as NSError {
            // Catch fires here, with an NSErrro being thrown
            print("error occurred, here are the details:\n \(error)")
        }

    }

 }

发生错误,详情如下:

Error Domain=NSCocoaErrorDomain Code=516 "无法执行该操作 完全的。 (可可错误 516。)“用户信息 = 0x7a67b680 {NSSourceFilePathErrorKey=/Users/User1/Library/Developer/CoreSimulator/Devices/0E591E5B-2E2F-4CCB-9099-95CE1EA3F557/data/Containers/Bundle/Application/E3C8FAE4-703D-46CA-AC37-A1C96A74E6BE/myApp.app/data。 xml, NSUserStringVariant=( 复制),NSFilePath=/Users/User1/Library/Developer/CoreSimulator/Devices/0E591E5B-2E2F-4CCB-9099-95CE1EA3F557/data/Containers/Bundle/Application/E3C8FAE4-703D-46CA-AC37-A1C96A74E6BE/myApp.app/数据.xml, NSDestinationFilePath=/Users/User1/Library/Developer/CoreSimulator/Devices/0E591E5B-2E2F-4CCB-9099-95CE1EA3F557/data/Containers/Data/Application/09F690B3-BDD8-4482-ADDE-E33F30D4B873/Documentsdata.xml, NSUnderlyingError=0x7a67dd80 "操作无法完成。 文件存在”}

请帮忙!

【问题讨论】:

    标签: ios iphone swift2 nsfilemanager xcode7


    【解决方案1】:

    实现NSFileManagerDelegate和委托方法

    optional func fileManager(_ fileManager: NSFileManager,
      shouldProceedAfterError error: NSError,
            copyingItemAtURL srcURL: NSURL,
                       toURL dstURL: NSURL) -> Bool
    

    并返回true

    你也可以使用

    func replaceItemAtURL(_ originalItemURL: NSURL,
                   withItemAtURL newItemURL: NSURL,
              backupItemName backupItemName: String?,
                            options options: NSFileManagerItemReplacementOptions,
              resultingItemURL resultingURL: AutoreleasingUnsafeMutablePointer<NSURL?>) throws
    

    【讨论】:

    • 谢谢@vadian,第二个选项有帮助!
    • 在 IOS 模拟器中测试我的应用程序时,复制文件过程正常,但是当我在我的设备 iPhone 上测试时,我收到错误:Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x17e8af80 {NSUnderlyingError=0x17e89210 "The operation couldn’t be completed. Operation not permitted", NSFilePath=/private/var/mobile/Containers/Bundle/Application/A44E5FF5-F52A-45E7-A803-DB6A19F1DB82/myApp.app/data.xml
    • 这是因为我使用 Xcode 7 侧载在 iPhone 上运行我的应用程序? @vadian
    • 没关系,我现在正在将文件写入文档目录,我可以从该位置处理它。
    猜你喜欢
    • 2015-12-18
    • 2014-12-13
    • 2013-10-03
    • 2023-03-28
    • 2011-04-05
    • 2013-03-17
    • 2016-04-22
    • 1970-01-01
    • 2011-09-02
    相关资源
    最近更新 更多