gpt4 book ai didi

ios - fatal error : unexpectedly found nil while unwrapping an Optional value Parse sign up

转载 作者:行者123 更新时间:2023-11-30 13:45:38 25 4
gpt4 key购买 nike

我正在尝试创建一个注册页面,但由于某种原因,当我在“let username = self.usernameField.text”行上不断收到相同的错误“ fatal error :展开可选值时意外发现nil”在模拟器中测试一下。我知道该错误与值为零有关,但无法弄清楚如何修复它,请帮助:(

代码=

@IBOutlet weak var emailField: UITextField?
@IBOutlet weak var usernameField: UITextField?
@IBOutlet weak var passwordField: UITextField?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

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

@IBAction func signUpAction(sender: AnyObject) {

let username = self.usernameField!.text
let password = self.passwordField!.text
let email = self.emailField!.text
let finalEmail = email!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

// Validate the text fields
if username?.characters.count < 5 {
let alert = UIAlertView(title: "Invalid", message: "Username must be greater than 5 characters", delegate: self, cancelButtonTitle: "OK")
alert.show()

} else if password?.characters.count < 8 {
let alert = UIAlertView(title: "Invalid", message: "Password must be greater than 8 characters", delegate: self, cancelButtonTitle: "OK")
alert.show()

} else if email?.characters.count < 8 {
let alert = UIAlertView(title: "Invalid", message: "Please enter a valid email address", delegate: self, cancelButtonTitle: "OK")
alert.show()

} else {
// Run a spinner to show a task in progress
let spinner: UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150)) as UIActivityIndicatorView
spinner.startAnimating()

let newUser = PFUser()

newUser.username = username
newUser.password = password
newUser.email = finalEmail

// Sign up the user asynchronously
newUser.signUpInBackgroundWithBlock({ (succeed, error) -> Void in

// Stop the spinner
spinner.stopAnimating()
if ((error) != nil) {
let alert = UIAlertView(title: "Error", message: "\(error)", delegate: self, cancelButtonTitle: "OK")
alert.show()

} else {
let alert = UIAlertView(title: "Success", message: "Signed Up", delegate: self, cancelButtonTitle: "OK")
alert.show()
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("Home")
self.presentViewController(viewController, animated: true, completion: nil)
})
}
})

最佳答案

试试这个...

import Foundation
import Parse
import UIKit


class LoginViewController: UIViewController {


@IBOutlet weak var userNameTF: UITextField!
@IBOutlet weak var passWordTF: UITextField!
@IBOutlet weak var emailTF: UITextField!



override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func SignUp(sender: AnyObject) {

signUp()
}

@IBAction func Login(sender: AnyObject) {
login()
}

func signUp() {

let user = PFUser()
user.username = userNameTF.text
user.password = passWordTF.text
user.email = emailTF.text

// other fields can be set if you want to save more information
// user["phone"] = "650-555-0000"

user.signUpInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if error == nil {
print("Sign Up Succesful")
} else {
print("Sign Up Failed")
}
}

}

func login() {
let user = PFUser()
user.username = userNameTF.text
user.password = passWordTF.text

PFUser.logInWithUsernameInBackground(userNameTF.text!, password: passWordTF.text!, block: {
(user: PFUser?, Error: NSError?) -> Void in

if Error == nil {
dispatch_async(dispatch_get_main_queue()) {
var Storyboard = UIStoryboard(name: "Main", bundle: nil)
var AfterLoginVC: UIViewController = Storyboard.instantiateViewControllerWithIdentifier("AfterLoginVC") as! UINavigationController
self.presentViewController(AfterLoginVC, animated: true, completion: nil)
}

} else {
print("Login Error, check credentials")
}

})

}

}

“AterLoginVC”是您想要在成功登录后实例化的 View Controller 的 Storyboard标识符。然后,您可以添加 if/else 语句来评估条件并相应地将错误返回给用户

关于ios - fatal error : unexpectedly found nil while unwrapping an Optional value Parse sign up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34977106/

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