gpt4 book ai didi

ios - Swift 1.2 解析保存在后台错误

转载 作者:可可西里 更新时间:2023-11-01 01:40:57 25 4
gpt4 key购买 nike

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

第一个错误出现在我的 .saveInBackgroundWithBlock 行:

函数签名“(Bool, NSError!) -> Void”与预期类型“@objc_block (Bool, NSError!) -> Void”不兼容

第二个位于出现注册错误时发生的 else 语句中,NSString { error = errorString }

“NSString”不能隐式转换为“String”;您是要使用“as”来显式转换吗?

完整代码如下:

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)

}
}


}


}

最佳答案

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

例如:

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

关于ios - Swift 1.2 解析保存在后台错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com