以Swift显示警报

以Swift显示警报,swift,xcode,Swift,Xcode,我正在学习swift,我正在为一个应用程序构建UI,我想知道我的代码有什么问题,因为它一直说我有错误 let userEmail = userEmailTextField.text; let userPassword = userPasswordTextField.text; let userRepeatPassword = repeatPasswordTextField.text; // check for empty fields if((userEmail.isEmpty || user

我正在学习swift,我正在为一个应用程序构建UI,我想知道我的代码有什么问题,因为它一直说我有错误

let userEmail = userEmailTextField.text;
let userPassword = userPasswordTextField.text;
let userRepeatPassword = repeatPasswordTextField.text;

// check for empty fields
if((userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty)
{

    //Display alert message
    displayMyAlertMessage("Alert fields are required");
    return;
}

//check if passwords match
if(userPassword != userRepeatPassword) 
// error expected expression in list of expression
{

    //Display alert message
    displayMyAlertMessage("Alert fields are required");
    return;
}  
应为“(在if条件之后

错误类型“UIAlertController”没有成员“Alert”


只需帮助您找出代码的错误

您的这一代码行:

var myAlert = UIAlertController(title:"Alert", message:
        userMessage, PrefferedStyle:UIAlertController.Alert); 
应该是:

var myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: .Alert);
Alert是枚举UIAlertControllerStyle的一种情况,因此它应该是UIAlertControllerStyle.Alert而不是UIAlertController.Alert。此外,由于编译器知道参数“preferredStyle”所期望的枚举类型,因此您可以改为使用.Alert

第一个if条件中的“(”than“)”更多:

if((userEmail.isEmpty || userPassword.isEmpty ||
        userRepeatPassword.isEmpty) {
从而导致第一个错误。您应该将其更改为:

if(userEmail.isEmpty || userPassword.isEmpty ||
            userRepeatPassword.isEmpty) {
if userEmail.isEmpty || userPassword.isEmpty ||
                userRepeatPassword.isEmpty {
为了遵循Swift编码惯例,您最终应将其更改为:

if(userEmail.isEmpty || userPassword.isEmpty ||
            userRepeatPassword.isEmpty) {
if userEmail.isEmpty || userPassword.isEmpty ||
                userRepeatPassword.isEmpty {
你不需要()在SWIFT。SWIFT会把它们看作元组。你可以做< /P>
if condition {


}

此外,在swift中的每条语句后面不需要分号。您可以删除

@Dat Hoang答案是正确的,但您的警报在显示之前实际上不会显示。在
displayMyAlertMessage
方法末尾添加这一行

present(self, animated: true, completion: nil)

我建议您也检查一下这一点

为什么“表达式列表中的错误预期表达式”行存在?如果是注释,则应以双斜杠字符开头。对于另一个错误,您拼错了“首选”。检查方法声明的拼写以确保调用正确。这很快。为什么在
if
语句中使用括号?这只是口味的问题。我只是告诉他错误的来源。我没有试图更改他的编码样式。我同意@rmaddyI agree with@rmaddy。对不起,伙计们。我同意t试图告诉他错误的来源。这实际上并不能解释OP代码的错误。而且它也不能解决
displayMyAlertMessage
函数中的问题。