无法将存储在Parse.com中的NSData转换为NSString?

时间:2015-03-06 14:21:35

标签: ios swift parse-platform

人 我需要将视频存储到Parse中,我这样做:

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

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

数据已成功存储到Parse中。然后我就这样得到它:

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


                    })
                }

在完成块中,我总是收到数据,但在尝试将其转换为NSString时,它总是为零。我想也许编码是问题,但没有任何作用。谢谢!

3 个答案:

答案 0 :(得分:1)

我找到了两个解决方案:

解决方案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)

                })

解决方案2:

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

                if let currentVideoFile = videoFile {

                    if currentVideoFile.url != nil {

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

                        }
                    }
                }

答案 1 :(得分:0)

使用此代码

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

使用NSUTF8StringEncoding代替

答案 2 :(得分:0)

如果您希望在swift中将pdf或其他UIView数据类型加载到webView中,则会略有不同。

          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)
                    }
                }