Swift – the Swift equivalent of isEqualToString in Objective-C

stringswift

I am trying to run the code below:

import UIKit

class LoginViewController: UIViewController {

@IBOutlet var username : UITextField = UITextField()
@IBOutlet var password : UITextField = UITextField()

@IBAction func loginButton(sender : AnyObject) {

    if username .isEqual("") || password.isEqual(""))
    {
        println("Sign in failed. Empty character")
    }
}

My previous code was in Objective-C, which was working fine:

 if([[self.username text] isEqualToString: @""] ||
    [[self.password text] isEqualToString: @""] ) {

I assume I cannot use isEqualToString in Swift. Any help would be appreciated.

Best Answer

With Swift you don't need anymore to check the equality with isEqualToString

You can now use ==

Example:

let x = "hello"
let y = "hello"
let isEqual = (x == y)

now isEqual is true.