Swift 2:“错误未处理”

时间:2015-09-21 11:59:00

标签: ios swift2

开始

  

“调用可以抛出,但它没有标记为尝试,错误不是   处理“

使用"contactUsObject.save()"中的swift2将Xcode从6.4更新为7后出现

错误。我怎样才能解决这个问题?

@IBAction func postCommentBtn_clicked(sender: AnyObject) {


    if messageTxtComment.text.isEmpty  {

        let alertView = UIAlertController(title: "Message field is empty", message: "Please enter a message to post", preferredStyle: .Alert)
        alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
        self.presentViewController(alertView, animated: true, completion: nil)

    }
    else {

        var theComment = messageTxtComment.text
        let len = messageTxtComment.text.utf16.count

        if len > 150 {

            theComment = theComment.substringToIndex(theComment.startIndex.advancedBy(150))

        }

        let contactUsObj = PFObject(className: "contactUsMessage")

        contactUsObj["username"] = PFUser.currentUser()!.username
        contactUsObj["email"] = PFUser.currentUser()!.valueForKey("email") as! String
        contactUsObj["name"] = nameField.text
        contactUsObj["enteredEmail"] = emailField.text
        contactUsObj["phoneNum"] = phoneField.text
        contactUsObj["message"] = theComment

        contactUsObj.save()


    }


}

2 个答案:

答案 0 :(得分:2)

do {
     try contactUsObj.save()
} catch error as NSError {
    print(error)
    //Handle any error.
}

答案 1 :(得分:0)

您需要使用do ... try ... catch语法来处理Swift 2.0错误处理。

替换你的:

contactUsObj.save()

像:

do
{
   try contactUsObj.save()
}
catch _
{
    // Handle error here
}

有关详细信息,请参阅Swift Error Handling