我正在寻求一些指导和/或帮助来解决我正在开发的 Swift 应用程序的密码注册/登录代码中遇到的两个错误。
我有点卡住了,因为我已经尝试了我能想到的并在网上找到的一切,但我似乎无法解决这个问题。我使用 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)
}
}
最佳答案
您有很多错误:
这个:
if(userEmail?.isEmpty)!, ||(userPassword?.isEmpty)!,|| (userRepeatPassword?.isEmpty)!
应该是:
if (userEmail?.isEmpty)!, (userPassword?.isEmpty)!, (userRepeatPassword?.isEmpty)!
最好避免因强制展开字符串而崩溃,并采用如下方式:
if userEmail != "", userPassword != "", userRepeatPassword != ""
请记住,将电子邮件与空字符串进行比较不足以进行有效注册。即使是带有空格的字符串也不是空的。
并且不要忘记对字符串文字使用双引号:
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)
}
关于swift - 一元运算符和未终止的字符串文字问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51592565/