简体   繁体   English

从String转换为NSURL始终返回nil

[英]Conversion From String to NSURL constantly returns nil

I am trying to fetch the path of an audio file that I have added to the main bundle of my app. 我正在尝试获取已添加到应用程序主捆绑包中的音频文件的路径。 When I print the path as String it prints normally. 当我将路径打印为String时,它将正常打印。

But, when I try to print the NSURL of the same after converting it, returns a nil 但是,当我尝试在转换后打印相同的NSURL时,返回nil

let audioPath: String = NSBundle.mainBundle().pathForResource("Test", ofType: "mp3")!
    print(audioPath)

    let audioPathURL: NSURL? = NSURL(string: audioPath)
    print(audioPathURL)

An URL in the file system must be initialized with the method NSURL(fileURLWithPath:) not NSURL(string:) . 文件系统中的URL必须使用NSURL(fileURLWithPath:)方法而非NSURL(string:)方法初始化。

Or you use the URL based methods of NSBundle 或者您使用基于URL的NSBundle方法

您需要使用特定于filePath的初始化程序

let audioPathURL: NSURL? = NSURL(fileURLWithPath: audioPath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM