【问题标题】:Unary Operator and Unterminated string literal issue一元运算符和未终止的字符串文字问题
【发布时间】:2019-01-06 14:54:49
【问题描述】:

我正在为我正在开发的 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)

}

}

【问题讨论】:

  • "OK' -> "OK"
  • if(userEmail?.isEmpty)!, ... 语句中删除逗号并在|| 运算符周围添加空格。注意:(userEmail?.isEmpty)!userEmail!.isEmpty 相同,如果userEmailnil,则两者都会崩溃。
  • 丢失;。 Swift 不是 C。
  • 谢谢马丁。不幸的是,这又产生了 4 个额外的错误。

标签: swift xcode string-literals unary-operator


【解决方案1】:

你有很多错误:

  1. 这个:

    if(userEmail?.isEmpty)!, ||(userPassword?.isEmpty)!,|| (userRepeatPassword?.isEmpty)!

应该是:

if (userEmail?.isEmpty)!, (userPassword?.isEmpty)!, (userRepeatPassword?.isEmpty)!

最好避免因强制解包字符串而崩溃并采用以下方式:

if userEmail != "", userPassword != "", userRepeatPassword != ""

请记住,将电子邮件与空字符串进行比较不足以进行有效注册。甚至带有空格的字符串也不是empty

  1. 不要忘记在字符串文字中使用双引号:

    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)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2014-06-09
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多