ios - ResetPassword 中警报中的可选文本 - 使用 Swift 的 iOS 项目

标签 ios swift alert

这是我的 iOS 应用程序使用 xcode 和 swift 的 ResetPassword 按钮:

//ResetPssword Button
@IBAction func ResetPassword(sender: AnyObject) {

    if validateEmail(EmailTextField.text!) ==  false {
        print("Enter a Valid Email Address")
        let VaildMessage = "Enter an Email Address"
        //Empty TextField Alert Message
        self.disaplayErrorMessage(VaildMessage)
   }

    //Reset
    else {

        ref.resetPasswordForUser(EmailTextField.text) { (ErrorType) -> Void in
            if ErrorType != nil {

                let error = ErrorType
                print("There was an error processing the request \(error.description)")
                let errorMessage:String = "The Email You Entered is not Exist"
                //Error Alert Message
                self.disaplayErrorMessage(errorMessage)

            } else {

                print("Password Reset Sent Successfully")
                let successMessage = "Email Message was Sent to You at \(self.EmailTextField.text)"

                //Success Alert Message
                self.disaplayErrorMessage(successMessage)
            }

    } //reset

    } //Big Else

 } //Button


//Display Alert Message With Confirmation
func disaplayErrorMessage(theMessage:String)
{
    //Display alert message with confirmation.
    let myAlert = UIAlertController(title: "Alert", message: theMessage, preferredStyle: UIAlertControllerStyle.Alert);

    let OkAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default) {
        action in
        self.dismissViewControllerAnimated(true, completion: nil);
    }
    myAlert.addAction(OkAction);
    self.presentViewController(myAlert, animated: true, completion: nil)
}


   //Validate Email Function
func validateEmail(candidate: String) -> Bool {
    let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
    return NSPredicate(format: "SELF MATCHES %@", emailRegex).evaluateWithObject(candidate)
}

如果密码重置发送成功,将会出现一条警告,打印出电子邮件地址。但它会打印带有可选单词的电子邮件!

enter image description here

如何在没有可选单词的情况下打印它?

最佳答案

您必须解开可选文本字段。

例如使用if let:

if let text = self.EmailTextField.text {
    let successMessage = "Email Message was Sent to You at \(text)"
    //Success Alert Message
    self.disaplayErrorMessage(successMessage)
} 

关于ios - ResetPassword 中警报中的可选文本 - 使用 Swift 的 iOS 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944483/

相关文章:

html - stringByReplacingOccurrencesOfString 无法正常运行

ios - CocoaPods升级到0.36.x及以上版本后如何引用Bridging-Header.h中的头文件?

swift - Startindex swift 3 问题

jsf - 执行托管 bean 操作方法后显示 alert()

ios - 使用 CGPattern 填充 MKPolygon 时,多边形叠加相互干扰(使用 Quartz 2D)

ios - 本地化 AB 地址簿

dictionary - 如何使用 Swift 枚举作为字典键? (符合等式)

swift - 在其他函数中使用 swift func vars

ios - 实现 UIAlertController 时遇到问题。无法提醒出现

javascript - 如果响应有效则显示 window.alert