How to get audio file from parse swift

I have successfully saved the audio file to Parse, but I am trying to upload it again. I cannot decide what the block should do with getDataInBackgroundWithBlock. And how to actually save the file. Any help is much appreciated!

let query = PFQuery(className: "wordRecordings")
query.whereKey("wordName", equalTo: "\(joinedWord)")

query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error:NSError?) -> Void in

    if error == nil{                        
       for object in objects! {
            let audioFile = object["audioFile"] as! PFFile
            audioFile.getDataInBackgroundWithBlock{ (audioFile: NSData, error:NSError?) -> Void in // THIS BLOCK ISNT CORRECT...
               if error == nil {
               let audioFile = AudioFile(data: audioFile) // THIS WORKS FOR IMAGES BUT NOT AUDIO - HOW DO I CREATE AN AUDIO FILE
               }
              }
             }
            } else {   
               print("Error: \(error!) \(error!.userInfo)")
           }
         }

Thanks to @David below is a working solution with path code.

let fileString = "\(fileName)"
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let path = documentsDirectory.URLByAppendingPathComponent(fileString)

audioFile.getDataInBackgroundWithBlock { (audioFile:NSData?, error:NSError?) -> Void in
         if error == nil {
           audioFile.writeToURL(path, atomically: true)
         } else {
         print("error with getData")
         }
  }
+2
source share
1 answer

Checking the official documentation should look something like this (I am not using Swift, so please bear with me):

audioFile.getDataInBackgroundWithBlock{ (audioFile: NSData?, error:NSError?) -> Void in
    if error == nil {
        let path = "path-to-your-file"
        if !audioFile.writeToFile(path, atomically: true){
            print("Error saving")
        }
    }
}

I would advise you to read some tutorials (for example, here ), where you can get the best examples of file processing in Swift.

Update

-, (. ). (audioFile: NSData?, error: NSError?)

+2

All Articles