Milen
Milen

Reputation: 81

Cannot convert NSData stored in Parse.com to NSString?

guys I need to store a video into Parse and I am doing it like this:

let videoData = NSData(contentsOfURL: videoURL)
self.videoFile = PFFile(data: videoData)

self.videoFile.saveInBackgroundWithBlock { (succeeded, error) -> Void    in
}

The data is successfully stored into Parse. Then I am getting it like this:

var videoFile:PFFile! = object[kParseUserVideo] as PFFile

                if let currentVideoFile = videoFile {

                    videoFile.getDataInBackgroundWithBlock({ (data, error) -> Void in

                        var videoString:NSString! = NSString(data: data, encoding:NSUTF32StringEncoding)

                        if let videoS = videoString {

                            let url = NSURL(string: videoS)

                            println(url)

                        } else {

                            println("ERROR!")
                        }

                        }, progressBlock: { (progress) -> Void in


                    })
                }

In the completion block I always receive the data but when trying to converted it to NSString, it is always nil. I think that maybe the encoding is the problem but nothing works. Thank you!

Upvotes: 0

Views: 351

Answers (3)

Mr. Roddy
Mr. Roddy

Reputation: 1

Slight variation if you wish to load a pdf or other UIView data types into a webView in swift.

          var pdfSelected = PFObject(className: "TestDataString")
                var pdfOutput:PFFile! = dataDownload?.objectForKey("pdfName") as! PFFile
                if let currentPDF = pdfOutput{
                    if currentPDF.url != nil{
                        let urlFromParse = NSURL(string: currentPDF.url!)
                        var request = NSURLRequest(URL:urlFromParse!)
                        self.webPresentation.loadRequest(request)
                    }
                }

Upvotes: 0

Milen
Milen

Reputation: 81

I found two solutions:

Solution 1:

                var videoFile:PFFile! = object[kParseUserVideo] as PFFile

                videoFile.getDataInBackgroundWithBlock({ (data, error) -> Void in

                    var videoString:NSString! = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

                    let url = NSURL(string: videoString)

                })

Solution 2:

                var videoFile:PFFile! = object[kParseUserVideo] as PFFile

                if let currentVideoFile = videoFile {

                    if currentVideoFile.url != nil {

                        let urlFromParse = NSURL(string: currentVideoFile.url!)

                        }
                    }
                }

Upvotes: 1

Fatti Khan
Fatti Khan

Reputation: 1553

Use this code

var returnData : NSString = NSString(data: responseData!, encoding: NSUTF8StringEncoding)

use NSUTF8StringEncoding instead

Upvotes: 0

Related Questions