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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31503283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:30:45  来源:igfitidea点击:

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

iosxcodeswiftdirectorynsurl

提问by Alexander Khitev

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 NSURLon specific file from my UITableViewCellas I pass names of these files. My files are mp3. Please help me.

我在NSFileManager. 我得到了一个文件名数组,我可以将它们传递给另一个控制器。当我传递这些文件的名称时,NSURL我仍然想传递我的特定UITableViewCell文件。我的文件是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(){ 
    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 
    }
}
.lastPathComponent }.filter(){
  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
        }
    }
.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.

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

My updated code

我更新的代码

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

UPDATE2I edited my code and I can to pass NSURLon mp3 files and in another UIViewControllerI can play them.

UPDATE2我编辑了我的代码,我可以传递NSURLmp3 文件,在另一个UIViewController我可以播放它们。

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

回答by zaph

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

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

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

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

folderPathURL.pathis incorrect.

folderPathURL.path是不正确的。

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

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

Example:

例子:

##代码##

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

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

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

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

With the intermediate statements each step can be validated.

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

回答by Alexander Khitev

I found a solution for my question

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

##代码##