I can't seem to convert NSURL to NSData. The NSURL appears when printed out, but then when I try to convert it to NSData, the audioData variable keeps returning nil.
func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!)
{
selectedSong = mediaItemCollection.items[0] as MPMediaItem
songUrl = selectedSong.valueForProperty(MPMediaItemPropertyAssetURL) as? NSURL
println("\(songUrl)")
audioData = NSData(contentsOfURL: songUrl) as NSData
println("\(audioData)")
}
Edited Code to Catch the Error
songUrl is the URL address of a song located on my iPod library
var errorPointer:NSErrorPointer!
audioData = NSData(contentsOfURL: songUrl, options: NSDataReadingOptions.DataReadingMappedAlways, error: errorPointer)
if audioData == nil
{
println("\(errorPointer)")
}
This code prints the error: "fatal error: unexpectedly found nil while unwrapping an Optional value" onto my console
Edit #2
Using the format in the error format in the answer below, I now get the error:
An Error Occurred: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x146f4a90 {NSURL=ipod-library://item/item.m4a?id=3210273692689505570}