二元运算符' =='不能应用于类型'表达式[String]的操作数?'和'字符串?'

时间:2015-12-03 05:33:58

标签: ios swift swift2

@IBAction func loginButtonTapped(sender:AnyObject){

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

    let userEmailStored = NSUserDefaults.standardUserDefaults().stringArrayForKey("userEmail");

    let userPasswordStored = NSUserDefaults.standardUserDefaults().stringArrayForKey("userPassword");

    if(userEmailStored == userEmail) // Binary operator '==' cannot be applied to operands of type 'Expression[String]?' and 'String?'
    {
        if(userPasswordStored == userPassword) // Binary operator '==' cannot be applied to operands of type 'Expression[String]?' and 'String?'
        {
            // Login Successfull

           NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn");
           NSUserDefaults.standardUserDefaults().synchronize();
           self.dismissViewControllerAnimated(true, completion: nil);
        }


    }


}

我遇到了上述问题。上述问题的解决方案是什么?感谢

2 个答案:

答案 0 :(得分:0)

您无法将数组与String进行比较。

尝试:

if userEmailStored.contains(userEmail) && userPasswordStored.contains(userPassword) {
    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn");
    NSUserDefaults.standardUserDefaults().synchronize();
    self.dismissViewControllerAnimated(true, completion: nil);
}

答案 1 :(得分:0)

let userEmailStored = NSUserDefaults.standardUserDefaults().stringArrayForKey("userEmail");
let userPasswordStored = NSUserDefaults.standardUserDefaults().stringArrayForKey("userPassword");

这两个代码有错误,函数是stringForKey,它不是stringArrayForKey。 String Array for key正在尝试构建字符串数组, 你无法将字符串数组与字符串进行比较。

NSUserDefaults.standardUserDefaults().stringForKey("userEmail");
NSUserDefaults.standardUserDefaults().stringForKey("userPassword");