4

How do you compare the text in two text fields to see if they are the same, such as in "Password" and "Confirm Password" text fields?

if (passwordField == passwordConfirmField) {

    //they are equal to each other

} else {

    //they are not equal to each other

}
jscs
  • 63,694
  • 13
  • 151
  • 195
Jack Humphries
  • 13,056
  • 14
  • 84
  • 125
  • 1
    Possible duplicate, see http://stackoverflow.com/questions/5071221/string-compare-objective-c also http://stackoverflow.com/questions/2592511/string-comparison-in-objective-c – KyleM Aug 01 '11 at 23:44
  • No, kind of close, but not exactly. I want to compare the text in TWO text fields to one another, not one text field to a specific string. – Jack Humphries Aug 01 '11 at 23:56
  • You know how to get the string out of one text field, right? So you get the second one the same way, and then you have two strings that you compare like any others. – jscs Aug 02 '11 at 00:52

2 Answers2

19

In Objective-C you should use isEqualToString:, like so:

if ([passwordField.text isEqualToString:passwordConfirmField.text]) {
    //they are equal to each other
} else {
    //they are *not* equal to each other
}

NSString is a pointer type. When you use == you are actually comparing two memory addresses, not two values. The text properties of your fields are 2 different objects, with different addresses.
So == will always1 return false.


In Swift things are a bit different. The Swift String type conforms to the Equatable protocol. Meaning it provides you with equality by implementing the operator ==. Making the following code safe to use:

let string1: String = "text"
let string2: String = "text"

if string1 == string2 {
    print("equal")
}

And what if string2 was declared as an NSString?

let string2: NSString = "text"

The use of == remains safe, thanks to some bridging done between String and NSString in Swift.


1: Funnily, if two NSString object have the same value, the compiler may do some optimization under the hood and re-use the same object. So it is possible that == could return true in some cases. Obviously this not something you want to rely upon.

Pierre Espenan
  • 3,996
  • 5
  • 33
  • 51
5

You can do this by using the isEqualToString: method of NSString like so:

NSString *password = passwordField.text;
NSString *confirmPassword = passwordConfirmField.text;

if([password isEqualToString: confirmPassword]) {
    // password correctly repeated
} else {
    // nope, passwords don't match
}

Hope this helps!

starbugs
  • 992
  • 1
  • 9
  • 14
  • This will actually check if the two memory addresses are equal, not the contents of the string... so it will always end up in the ELSE – Craig Stanford Aug 01 '11 at 23:48
  • Are you sure ? From what I understand from the doc, isEqual: will compare the hash of to object. If you use it to compare 2 string objects, it will work. But knowing the two objects are strings, it's faster to use isEqualToString: – Pierre Espenan Aug 01 '11 at 23:51
  • Nope, that's not true. isEqual won't compare memory addresses. It is implemented by the corresponding object it is sent to. In the case of NSString it does compare the contents of both strings. See http://stackoverflow.com/questions/1292862/nsstring-isequal-vs-isequaltostring. – starbugs Aug 01 '11 at 23:52
  • True, isEqualToString is faster. You should prefer using that. – starbugs Aug 01 '11 at 23:54
  • I stand corrected! I just always associate isEqual: with comparing objects, not their contents... – Craig Stanford Aug 01 '11 at 23:55