嗨社区我已经对此问题进行了一段时间的排查,并且没有在网络上找到与我的问题有关的解决方案。
我对快速语言不熟悉我认为这个问题似乎与将NSString转换为String有关。
错误读取
无法转换类型' NSString的值?'预期的参数类型'字符串?'
关于我的
let file = PFFile(name: fileName, data: fileData!, contentType: fileType )
这是整个功能
func uploadMessage() {
var fileData: NSData?
var fileName: NSString?
var fileType: NSString?
if image != nil {
let newImage = resizeImage(image!, width: view.window!.frame.size.width, height: view.window!.frame.size.height)
fileData = UIImagePNGRepresentation(newImage)
fileName = "image.png"
fileType = "image"
} else {
fileData = NSData.dataWithContentsOfMappedFile(videoFilePath! as String) as? NSData
fileName = "video.mov"
fileType = "video"
}
let file = PFFile(name: fileName, data: fileData!, contentType: fileType )
file.saveInBackgroundWithBlock { (success: Bool, error: NSError!) -> Void in
if error == nil {
let message = PFObject(className: "Messages")
message.setObject(file, forKey: "file")
message.setObject(fileType!, forKey: "fileType")
message.setObject(self.recipients!, forKey: "recipientIds")
message.setObject(PFUser.currentUser()!.objectId!, forKey: "senderId")
message.setObject(PFUser.currentUser()!.username!, forKey: "senderName")
message.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
if error == nil {
//worked!
self.reset()
} else {
UIAlertView(title: "Error", message: "Try agian", delegate: nil, cancelButtonTitle: "OK").show()
}
})
} else {
UIAlertView(title: "Error", message: "Try agian", delegate: nil, cancelButtonTitle: "OK").show()
}
}
先谢谢社区。 p>
答案 0 :(得分:3)
错误很明显......要么让你的fileName
变量成为一个字符串,(因为你使用的是Swift,你可能应该这么做),或者当你要设置文件时,将NSString
转换为String
let file = PFFile(name: fileName as String, data: fileData!, contentType: fileType)
原始答案仍然有效,但您必须执行以下任一选项:
do {
let file = try PFFile(name: fileName as String, data: fileData!, contentType: fileType)
} catch (_) {
//this is the default catch which is exhaustive and will catch all errors. here you can find the specific error if you want, or just use this as a way to know the file wasn't created and to let the user know
}