本文介绍了String(data:data,encoding:NSUTF8StringEncoding)return nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSQMessage和pare发送和保存视频文件

I am using JSQMessage and pare to send and save video files

if (mediaType == kUTTypeVideo as String) || (mediaType ==  kUTTypeMovie as String) {

        let movieURL = info[UIImagePickerControllerMediaURL] as? NSURL
        let videoMediaItem = JSQVideoMediaItem.init(fileURL: movieURL, isReadyToPlay: true)
        let message = JSQMessage(senderId: self.senderId, senderDisplayName: senderDisplayName, date: NSDate(), media: videoMediaItem)
        self.saveMediaToParse(message, data: NSData(contentsOfURL: movieURL!)!, fileName:"videoFile")
        self.messages += [message]
        self.publishMessageToChannel(message)
        self.finishSendingMessage()
    }  


func saveMediaToParse(message:JSQMessage, data:NSData, fileName:String) {

    let imageFile = PFFile.init(data: data)
    let msg = PFObject(className: "test")
    msg["senderId"] = message.senderId
    msg[fileName] = imageFile
    msg["senderDisplayName"] = message.senderDisplayName
    msg["user"] = PFUser.currentUser()

    msg.saveInBackgroundWithBlock { (success, error) -> Void in
        if(error != nil) {
            NSLog("error: %@",(error?.localizedDescription)!)
        }
    }
}

当我尝试从数据中获取视频URL时,我返回零值。我做错了什么?

when I am trying to get the video URL from the data as String I get back nil value. What am I doing wrong?

message["videoFile"].getDataInBackgroundWithBlock({ (data, error) -> Void in
                        if let data = data where error == nil {
                            **let dataStr = String(data: data, encoding: NSUTF8StringEncoding)**

                            let movieUrl = NSURL(string: dataStr!)
                            let videoMediaItem = JSQVideoMediaItem.init(fileURL: movieUrl, isReadyToPlay:true)
                            jsqMessage = JSQMessage(senderId:message["senderId"] as! String, displayName: message["senderDisplayName"] as! String, media: videoMediaItem)
                            self.messages.append(jsqMessage)
                            self.collectionView?.reloadData()
                            self.scrollToBottomAnimated(true)
                        }
                    })
                }
                self.messages.append(jsqMessage)


推荐答案

结束做
让dataStr = NSTemporaryDirectory()+temp.mov
data.writeToFile(dataStr,atomically:true)
let movieUrl = NSURL(fileURLWithPath:dataStr)

Ended up doing let dataStr = NSTemporaryDirectory() + "temp.mov" data.writeToFile(dataStr, atomically: true) let movieUrl = NSURL(fileURLWithPath: dataStr)

而不是
让dataStr = String(data:data,encoding:NSUTF8StringEncoding)**

Instead of let dataStr = String(data: data, encoding: NSUTF8StringEncoding)**

这篇关于String(data:data,encoding:NSUTF8StringEncoding)return nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 00:08