嘿,社区我已经解决这个问题有一段时间了,还没有在网上找到一个与我的问题特别相关的解决方案。
我是swift语言的新手,我认为这个问题似乎与将NSString转换成String有关。
错误如下
无法转换'NSString'类型的值需要参数类型“String?”
关于我的
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()
}
}
提前感谢社区。
最佳答案
这个错误很明显。。。要么将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
}
关于ios - 可以抛出调用,但未将其标记为“try”,并且未处理错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34294901/