1

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}

user3353890
  • 1,844
  • 1
  • 16
  • 31
  • use [`NSData(contentsOfURL: options: error:)`](https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/#//apple_ref/occ/instm/NSData/initWithContentsOfURL:options:error:) and let us know what the error returned is. – Michael Dautermann Oct 23 '14 at 00:06
  • Thanks for the reply...I wrote your code like so: audioData = NSData(contentsOfURL: songUrl, options: NSDataReadingOptions.DataReadingMappedAlways, error: NSErrorPointer()) although i'm not sure I initialized the errorpointer correctly, there wasn't any error printout on my console. – user3353890 Oct 23 '14 at 00:12
  • There shouldn't be any output on the console. You need to log error. – rdelmar Oct 23 '14 at 00:28
  • @rdelmar cool, thanks for the tip, I'll try that out and get back to you – user3353890 Oct 23 '14 at 00:37
  • Yes, do not declare "`NSErrorPointer()`" *within* the `contentsOfURL` call. Declare it outside, pass it in via `contentsOfURL` *as a parameter* and then print out the error if `audioData` comes back nil. – Michael Dautermann Oct 23 '14 at 00:39
  • What is `songUrl`, add that to your question. – zaph Oct 23 '14 at 00:45
  • Go and read-up on error parameters. You really do need to know about them, they are quite helpful, even quicker than a SO question. – zaph Oct 23 '14 at 00:49
  • See the code in the answer below, an answer only for the formatting. – zaph Oct 23 '14 at 01:01
  • @Zaph I've been trying for a while now to figure out what Cocoa Error 256 is, but I haven't had any luck yet. Do you have any suggestions? – user3353890 Oct 23 '14 at 01:59
  • The URL is only appropriate with AVFoundation; NSData won't be able to do a thing with the NSURL. From Apple docs: "Usage of the URL outside of the AV Foundation framework is not supported." – zaph Oct 23 '14 at 12:43

1 Answers1

0

The URL is only appropriate with AVFoundation, NSData won't be able to do a thing with the NSURL.
From Apple docs:

"Usage of the URL outside of the AV Foundation framework is not supported."

Example of how to code the error parameter:

var error: NSError?
audioData = NSData(contentsOfURL: songUrl, options: .DataReadingUncached, error: &error)

if audioData == nil {
    if let unwrappedError = error {
        println("An Error Occurred: \(unwrappedError)")
    }
}
zaph
  • 111,848
  • 21
  • 189
  • 228
  • ugh, i'm sorry, i'll try again. The whole NSErrorPointer threw me off a bit, I thought it was asking for something else as opposed to a regular NSError. Thanks for working with me. – user3353890 Oct 23 '14 at 01:03
  • Ok, now I think I have the correct error...The program prints unwrappedError as "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}" – user3353890 Oct 23 '14 at 01:06