How to get NSURL on specific file from document directory in swift

半城伤御伤魂 提交于 2019-12-06 15:53:47

问题


I getting files from document directory with the help of NSFileManager. I got an array of file names and I can pass them to another controller. Still I want to pass NSURL on specific file from my UITableViewCell as I pass names of these files. My files are mp3. Please help me.

 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 
    }
}

UPDATE:

I updated my code and I can to get files of jpg type. But I don't know how to get mp3 files.

My updated code

    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 
    }
}

UPDATE2 I edited my code and I can to pass NSURL on mp3 files and in another UIViewController I can play them.

Below there is a edited code

  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:


Create the full file URL in a separate intermediate statement and you will find the problem.

If you do not have independent statements debugging is very hard.

folderPathURL.path is incorrect.

First create the entire file path, then create f file URL from it.

Example:

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!)")

Output:

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

filePath: /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/Documents/aFileName

With the intermediate statements each step can be validated.




回答2:


I found a solution for my question

  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
    }
}


来源:https://stackoverflow.com/questions/31503283/how-to-get-nsurl-on-specific-file-from-document-directory-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!