swift - 一元运算符和未终止的字符串文字问题

标签 swift xcode string-literals unary-operator

我正在寻求一些指导和/或帮助来解决我正在开发的 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)

}

}

最佳答案

您有很多错误:

  1. 这个:

    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);
    

    假设 userNameuserMobile 将在其他地方使用,则以下代码应编译无错误:

    @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/

    相关文章:

    swift - 导航 View 将全屏转换为具有角半径的 View

    xcode - 如何以编程方式启动/停止仪器(时间分析器)?

    ios - 澄清 XCode 中的 StoryBoard 引用

    ios - 添加 subview 时屏幕失真

    swift - 如何刷新 TableView 中的特定单元格并存储更改的数据以便稍后在同一 TableView 中显示

    ios - 我想将单选按钮方法与 swift 中的选择器语句连接起来

    string - 使用ANTLR解析字符串文字时,出现NoViableAltException

    java - 私有(private)静态方法中的最终字符串在调用时是否会实例化一个新对象?

    swift - 如何在使用我的 Mac 应用程序的查找器中添加上下文菜单

    string - Go 中对字符串字面量的引用