我正在快速学习,我正在为应用程序构建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 if condition
func displayMyAlertMessage(userMessage: String)
{
var myAlert = UIAlertController(
title: "Alert",
message: userMessage,
PrefferedStyle:UIAlertController.Alert);
}
错误类型“UIAlertController'没有成员'警报'
需要帮助找出代码的错误
答案 0 :(得分:2)
您的代码行:
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) {
要遵循Swift编码惯例,最后应将其更改为:
if userEmail.isEmpty || userPassword.isEmpty ||
userRepeatPassword.isEmpty {
答案 1 :(得分:0)
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
}
在swift中if语句之后你不需要()。斯威夫特可能会将它们视为元组。你可以做到
if condition {
}
此外,您在swift中的每个语句后都不需要分号。您可以删除;
答案 2 :(得分:0)
displayMyAlertMessage
方法的末尾添加此行
present(self, animated: true, completion: nil)
我建议您查看此Swift Coding Styling Guide