我在尝试创建一个函数来检查用户输入和存储数据时遇到了上述错误。我的项目构建良好,直到我达到这个函数RegisterButtonTapped()
。有没有人对结构或语法进行了一些更改,可以消除这个错误?
@IBAction func RegisterButtonTapped(sender: AnyObject) {
let userEmail = userEmailTextField.text;
let userPassword = userEmailTextField.text;
let userRepeatPassword = userRepeatPasswordTextField.text;
// Check for empty fields
if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty){
displayAlertMessage("All fields are required");
return;
}
// Check if passwords match
if(userPassword != userRepeatPassword){
displayAlertMessage("Passwords do not match");
return;
}
// Store Data
NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userEmail");
NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userPassword");
NSUserDefaults.standardUserDefaults().synchronize();
// Display Alert message with confirmation
var myAlert = UIAlertController(title:"Alert",message:"Registration is successful, thank you.",preferredStyle: UIAlertControllerStyle.Alert);
func displayAlertMessage(userMessage:String){
var myAlert = UIAlertController(title:"Alert",message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title:"ok",style: UIAlertActionStyle.Default, handler:nil);
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil);
} // END OF FUNCTION 'displayAlertMessage()'
} // END of FUNCTION 'RegisterButtonTapped()'
当你有一个嵌套函数时,你必须在调用它之前声明它。在这种情况下,将"displayAlertMessage(userMessage:String)"函数移到"//检查空字段"注释上方,然后它应该编译。
调用displayAlertMessage后,如果要将其保留为嵌套函数,请将其声明移到RegisterButtonTapped()顶部附近,否则将其移出RegisterButtonTapped(()。
除此之外,您有两个变量都被称为myAlert,第一个是无用的,您将userEmail保存为电子邮件和密码,也不需要调用synchronize()。
有一些细节可以让它正常运行。displayAlertMessage
是在调用之后在register函数中声明的,类似地,我们得到了警告。当您获得成功时,您必须调用带有成功消息的函数,而不是像在alert函数中那样声明var myAlert
。最后一个细节:当获取UITextFields值时,您获得了电子邮件输入字段,并将其设置为密码值,因此验证将是错误的。
这里有一个运行良好的代码示例:
class ViewController: UIViewController {
@IBOutlet var userEmailTextField:UITextField!
@IBOutlet var userPassTextField:UITextField!
@IBOutlet var userRepeatPasswordTextField:UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func RegisterButtonTapped(sender:UIButton) {
let userEmail = userEmailTextField.text;
let userPassword = userPassTextField.text;
let userRepeatPassword = userRepeatPasswordTextField.text;
// Check for empty fields
if(userEmail!.isEmpty || userPassword!.isEmpty || userRepeatPassword!.isEmpty){
displayAlertMessage("All fields are required");
return;
}
// Check if passwords match
if(userPassword != userRepeatPassword){
displayAlertMessage("Passwords do not match");
return;
}
// Store Data
NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userEmail");
NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "userPassword");
NSUserDefaults.standardUserDefaults().synchronize();
displayAlertMessage("Registration is successful, thank you.")
}
// Display Alert message with confirmation
func displayAlertMessage(userMessage:String){
let myAlert = UIAlertController(title:"Alert",message: userMessage, preferredStyle: UIAlertControllerStyle.Alert);
let okAction = UIAlertAction(title:"ok",style: UIAlertActionStyle.Default, handler:nil);
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated: true, completion: nil);
}
}