简体   繁体   中英

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.

But, when I try to print the NSURL of the same after converting it, returns a 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:) .

Or you use the URL based methods of NSBundle

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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