gilbert105
gilbert105

Reputation: 13

Call can throw, but it is not marked with 'try' and the error is not handled

Hey community I've been troubleshooting this issue for a while now and haven't found a solution on the web that relates to my issue specifically.

I'm new to the swift language I'm assuming this problem seems to have something to do with converting NSString to a String.

the error reads

Cannot convert value of type 'NSString?' to expected argument type 'String?'

In regards to my

let file = PFFile(name: fileName, data: fileData!, contentType: fileType )

here is the entire function

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

Thanks in advance community.

Upvotes: 0

Views: 509

Answers (1)

pbush25
pbush25

Reputation: 5248

Well the error is pretty clear... Either make your fileName variable a string, (which since you're using Swift you should probably be doing anyways), or when you're about to set the file, convert the NSString into a String by let file = PFFile(name: fileName as String, data: fileData!, contentType: fileType)

Original answer still stands, but you'll have to do either one of the options like so:

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
}

Upvotes: 3

Related Questions