如何在swift中从文档目录中获取特定文件的NSURL

Posted

技术标签:

【中文标题】如何在swift中从文档目录中获取特定文件的NSURL【英文标题】:How to get NSURL on specific file from document directory in swift 【发布时间】:2015-07-19 16:21:32 【问题描述】:

我在NSFileManager 的帮助下从文档目录中获取文件。我得到了一组文件名,我可以将它们传递给另一个控制器。当我传递这些文件的名称时,我仍然想从我的UITableViewCell 传递特定文件上的NSURL。我的文件是 mp3。请帮帮我。

 var listOfMP3Files: Array<String!>? // for Cell data

    func fetchFilesFromFolder() 
        var fileManager = NSFileManager.defaultManager()

        var folderPathURL = fileManager.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)[0] as! NSURL

        var documentsContent = fileManager.contentsOfDirectoryAtPath(folderPathURL.path!, error: nil)
        println(documentsContent)

        if var directoryURLs = fileManager.contentsOfDirectoryAtURL(folderPathURL, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil) 
            println(directoryURLs)

            var mp3Files = directoryURLs.map() $0.lastPathComponent .filter() $0.pathExtension == "mp3" 

            listOfMP3Files = mp3Files
            println(mp3Files)
        
    

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! AVPlayerVC
    var indexPath = tableView.indexPathForSelectedRow()
    var objectForPass = listOfMP3Files![indexPath!.row] // default
    if segue.identifier == "listenMusic" 
        playerVC.object = objectForPass 
    

更新

我更新了我的代码,我可以获取 jpg 类型的文件。但我不知道如何获取 mp3 文件。

我更新的代码

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
    var indexPath = tableView.indexPathForSelectedRow()
    var objectForPass = listOfMP3Files![indexPath!.row] // default
    //

    var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as? String
    var getImage = path?.stringByAppendingPathComponent(objectForPass)
    var image = UIImage(contentsOfFile: getImage!)

    if segue.identifier == "listenMusic" 
        playerVC.musicFile = objectForPass
        playerVC.end = image 
    

更新2 我编辑了我的代码,我可以在 mp3 文件中传递 NSURL,在另一个 UIViewController 中我可以播放它们。

下面是编辑过的代码

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
        var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
        var indexPath = tableView.indexPathForSelectedRow()
        var objectForPass = listOfMP3Files![indexPath!.row] // default
        //

      /*  var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as? String
        var getImage = path?.stringByAppendingPathComponent(objectForPass)
        var image = UIImage(contentsOfFile: getImage!) */
        //
        var fileM = NSFileManager.defaultManager()

        var wayToFile = fileM.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
        var passMusicFileURL: NSURL?

        if let documentPath: NSURL = wayToFile.first as? NSURL 
            let musicFile = documentPath.URLByAppendingPathComponent(objectForPass)
            println(musicFile)
            passMusicFileURL = musicFile
        
        if segue.identifier == "listenMusic" 
           // playerVC.musicFile = objectForPass
            playerVC.end = passMusicFileURL
        
    

【问题讨论】:

【参考方案1】:

在单独的中间语句中创建完整的文件 URL,你会发现问题。

如果你没有独立的语句调试是非常困难的。

folderPathURL.path 不正确。

首先创建整个文件路径,然后从中创建 f 文件 URL。

例子:

let name = "aFileName"
var filePath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
println("\nfilePath: \(filePath)")

filePath = filePath.stringByAppendingPathComponent(name)
println("\nfilePath: \(filePath)")

var filePathURL = NSURL.fileURLWithPath(filePath)
println("\nfilePathURL: \(filePathURL!)")

输出:

文件路径:/Users/zaph/Library/Developer/CoreSimulator/Devices/264B31F6-3A28-4C75-9205-3558BBF54DED/data/Containers/Data/Application/0048403F-C8DE-4EE7-83CF-6AE7CCBAF090/Documents

文件路径:/Users/zaph/Library/Developer/CoreSimulator/Devices/264B31F6-3A28-4C75-9205-3558BBF54DED/data/Containers/Data/Application/0048403F-C8DE-4EE7-83CF-6AE7CCBAF090/Documents/aFileName

filePathURL:file:///Users/zaph/Library/Developer/CoreSimulator/Devices/264B31F6-3A28-4C75-9205-3558BBF54DED/data/Containers/Data/Application/0048403F-C8DE-4EE7-83CF-6AE7CCBAF090/文件/aFileName

使用中间语句可以验证每个步骤。

【讨论】:

【参考方案2】:

我为我的问题找到了解决方案

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
    var playerVC = (segue.destinationViewController as! UINavigationController).topViewController as! PlayMusicViewController
    var indexPath = tableView.indexPathForSelectedRow()
    var objectForPass = listOfMP3Files![indexPath!.row] // default
    //

  /*  var path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as? String
    var getImage = path?.stringByAppendingPathComponent(objectForPass)
    var image = UIImage(contentsOfFile: getImage!) */
    //
    var fileM = NSFileManager.defaultManager()

    var wayToFile = fileM.URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
    var passMusicFileURL: NSURL?

    if let documentPath: NSURL = wayToFile.first as? NSURL 
        let musicFile = documentPath.URLByAppendingPathComponent(objectForPass)
        println(musicFile)
        passMusicFileURL = musicFile
    
    if segue.identifier == "listenMusic" 
       // playerVC.musicFile = objectForPass
        playerVC.end = passMusicFileURL
    

【讨论】:

以上是关于如何在swift中从文档目录中获取特定文件的NSURL的主要内容,如果未能解决你的问题,请参考以下文章

如何在MongoDB中从某些类别中查找特定文档

在 Swift 中从 Firestore 中获取文档列表

如何在 Swift 中从文件夹中获取 UIImage 数组?

如何在swift3中从库的PHAsset中获取图像文件名

如何在 swift 5.3 中从 m4a 音频文件中获取 Track Id

如何在 Swift 中从 NSData 显示 PDF - 如何将 PDF 保存到文档文件夹 Swift - 如何在 Swift 中通过 WebView 从保存的 NSData 显示 PDF