我正在寻找有关我正在使用的Swift应用的密码注册/登录代码中的这2个错误的指导和/或帮助。
由于尝试了所有可以想到的内容并在网上找到了我,所以我有些困惑,但是我似乎无法破解这个问题。我使用Swift 1和2做了大量工作,但是我认为这可能与Swift 4的语法以及从3/4开始的更改有关。
将不胜感激;-)
//
// RegisterPageViewController.swift
// v2.0.2
//
// Created by Alex Jeffries on 18/07/2018.
//
import UIKit
class RegisterPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBOutlet weak var userFullNameTextField: UITextField!
@IBOutlet weak var userEmailTextField: UITextField!
@IBOutlet weak var userMobileNumberField: UITextField!
@IBOutlet weak var userPasswordTextField: UITextField!
@IBOutlet weak var repeatPasswordTextField: UITextField!
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func registerButtonTapped(_ sender: Any) {
let userName = userFullNameTextField.text;
let userEmail = userEmailTextField.text;
let userMobile = userMobileNumberField.text;
let userPassword = userPasswordTextField.text;
let userRepeatPassword = repeatPasswordTextField.text;
// Check for empty fields
if(userEmail?.isEmpty)!, ||(userPassword?.isEmpty)!,|| (userRepeatPassword?.isEmpty)!
{
//Display alert message
displayMyAlertMessage(userMessage: "All fields are required");
return;
}
//Check if passwords match
if(userPassword != userRepeatPassword)
{
//Display an alert message
displayMyAlertMessage(userMessage: "Passwords do not match");
return;
}
// Store data
// Display alert message with confirmation
}
func displayMyAlertMessage(userMessage:String)
{
_ = UIAlertController(title:"Alert", message: userMessage, preferredStyle:
UIAlertControllerStyle.alert);
_ = UIAlertAction(title:"OK', style:UIalertActionStyle.Default, handler:nil);
myAlert.addAction(okAction);
self.presentViewController(myAlert, animated:true, completion:nil)
}
}
答案 0 :(得分:0)
您有很多错误:
此:
if(userEmail?.isEmpty)!, ||(userPassword?.isEmpty)!,||
(userRepeatPassword?.isEmpty)!
应该是:
if (userEmail?.isEmpty)!, (userPassword?.isEmpty)!, (userRepeatPassword?.isEmpty)!
最好避免使用强制展开的字符串而崩溃,并执行以下操作:
if userEmail != "", userPassword != "", userRepeatPassword != ""
请记住,将电子邮件与空字符串进行比较不足以进行有效注册。甚至带有空格的字符串也不是empty
。
并且不要忘记在字符串文字中使用双引号:
let okAction = UIAlertAction(title:"OK', style: UIAlertActionStyle.default, handler:nil);
应该是:
let okAction = UIAlertAction(title:"OK", style: UIAlertActionStyle.default, handler:nil);
假设userName
和userMobile
将在其他地方使用,则以下代码应无错误编译:
@IBAction func registerButtonTapped(_ sender: Any) {
let userName = userFullNameTextField.text;
let userEmail = userEmailTextField.text;
let userMobile = userMobileNumberField.text;
let userPassword = userPasswordTextField.text;
let userRepeatPassword = repeatPasswordTextField.text;
// Check for empty fields
if userEmail != "", userPassword != "", userRepeatPassword != ""
{
//Display alert message
displayMyAlertMessage(userMessage: "All fields are required");
return;
}
//Check if passwords match
if(userPassword != userRepeatPassword)
{
//Display an alert message
displayMyAlertMessage(userMessage: "Passwords do not match");
return;
}
// Store data
// Display alert message with confirmation
}
func displayMyAlertMessage(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.present(myAlert, animated:true, completion:nil)
}