Swift 1.2解析后台存储错误

时间:2015-04-23 15:27:06

标签: ios swift parse-platform

我最近切换到Swift 1.2并注意到它打破了我的Parse功能的少量。我仍然试图解决为什么版本更新打破了这些最初的工作方法以及我应该如何解决它们。我看到的两个错误都在同一个.saveInBackgroundWithBlock函数中。

第一个错误发生在.saveInBackgroundWithBlock行:

Function signature '(Bool, NSError!) -> Void' is not compatible with expected type '@objc_block (Bool, NSError!) -> Void'

第二个位于else语句中,如果存在注册错误,NSString { error = errorString }

'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert?

以下是完整代码:

import UIKit

class UserRegistrationViewController: UIViewController {


    func displayAlert(title:String, error:String) {

        var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: {
            action in



        }))

        self.presentViewController(alert, animated: true, completion: nil)


    }

    @IBOutlet var usernameTextField: UITextField!

    @IBOutlet var emailTextField: UITextField!

    @IBOutlet var passwordTextField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func registerUser(sender: AnyObject) {

        var error = ""

        if usernameTextField.text == nil || emailTextField.text == nil || passwordTextField.text == nil {

            error = "Please enter a username, email and password"

        }


        if error != "" {

            displayAlert("Error In Form", error: error)

        } else {

            var user = PFUser.currentUser()

            user.username = usernameTextField.text
            user.password = passwordTextField.text
            user.email = emailTextField.text

            user.saveInBackgroundWithBlock {
                (succeeded: Bool!, signupError: NSError!) -> Void in
                if signupError == nil {

                    println(user.username)
                    println(user.password)
                    println(user.email)


                    self.performSegueWithIdentifier("successfulRegistration", sender: self)

                    /*self.navigationController?.setNavigationBarHidden(self.navigationController?.navigationBarHidden == false, animated: true)*/

                } else {

                    if let errorString = signupError.userInfo?["error"] as? NSString {
                        error = errorString
                    } else {

                        error = "Please try again later."

                    }


                    self.displayAlert("Could Not Sign Up", error: error)

                }
            }


        }


    }

1 个答案:

答案 0 :(得分:2)

首先,NSError需要是可选的,而不是隐式解包的

例如:

.saveInBackgroundWithBlock( { (success: Bool, error: NSError?) -> Void in