【问题标题】:appendingPathComponent' is unavailable: Use appendingPathComponent on URL instead.aappendingPathComponent' 不可用:改为在 URL 上使用 appendingPathComponent。a
【发布时间】:2017-12-20 02:55:48
【问题描述】:

我正在使用 Swift 4、Xcode 9 和开发目标 iOS 11.0。

我正在尝试将自定义文件夹 (MyFolder) 附加到 path 变量。

let outputFilePath = (NSTemporaryDirectory() as NSString).appending("MyFolder").appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)

但是builder给出了错误信息:

appendingPathComponent' 不可用:在 URL 上使用 appendingPathComponent。

我知道,我犯了一些愚蠢的错误。你能帮我解决这个问题吗?

【问题讨论】:

  • 你为什么要在你的 Swift 代码中使用NSString
  • let outputFileURL = FileManager.default.temporaryDirectory.appendingPathComponent(outputFileName).appendingPathExtension("mov") 如果你需要路径let outputFilePath = outputFileURL.path
  • 为什么需要子文件夹来保存临时文件?
  • 实际上我正在尝试将照片保存在我的相册旁边的自定义文件夹中
  • 我的相册是什么?它是位于 Documents 目录中的文件夹吗?还是它位于您的捆绑包中?您在上面发布的代码试图将其保存在一个临时目录中,我们甚至不知道该子文件夹是否存在。

标签: ios swift


【解决方案1】:

使用这条线

URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("MyFolder").appendingPathComponent(outputFileName).appendingPathExtension("mov")

而不是

(NSTemporaryDirectory() as NSString).appending("MyFolder").appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)

这将返回一个 url 并使用 url.Pathstring 中获取其路径。 希望对您有所帮助。

【讨论】:

    【解决方案2】:

    查看以下代码以在文档目录中参考

    class func getDocumentsDirectory() -> URL {
            let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            let dataPath = documentsDirectory.appendingPathComponent("FolderName")
    
            do {
                try FileManager.default.createDirectory(atPath: dataPath.path, withIntermediateDirectories: true, attributes: nil)
            } catch let error as NSError {
                print("Error creating directory: \(error.localizedDescription)")
            }
            return dataPath
        }
    

    用于在文件夹中附加文件您可以使用此

    //name for file to be added 
    let uuid = UUID().uuidString
    // storing a Audio File in Directory 
    let audioFilename = getDocumentsDirectory().appendingPathComponent("\(uuid).m4a")
    

    获取创建的相关文件夹中可用文件的名称

        //This function returns a Array with file names Available
        class func getListOfRecordingsAvailable() -> [String] {
                var fileNameArray = [String]()
                let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
                let myFilesPath = documentDirectoryPath.appending("/FolderName")
                let files = FileManager.default.enumerator(atPath: myFilesPath)
    
                while let file = files?.nextObject() {
                    //myfilesPath - Path
                    //file - fileName
                    fileNameArray.append(file as! String)
                }
    
                print(fileNameArray)
                return fileNameArray
    
    
    }
    

    【讨论】:

    • 根据错误信息和API演变Apple强烈建议使用FileManager的URL相关API,例如createDirectory(at:withIntermediateDirectories:attributes:)contentsOfDirectory(at:includingPropertiesForKeys:options:)enumerator(atPath 已过时。
    猜你喜欢
    • 2021-05-04
    • 1970-01-01
    • 2020-06-18
    • 2017-12-18
    • 2018-05-17
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多