-1

Possible Duplicate:
If “a == b” is false when comparing two NSString objects?

Simple Question of comparing 2 strings:

if (string1 == string2)
{
    NSLog(@"it is equal!");
}

thats it but it wont work for me it is always != i testet it with string2 = string1; but it wont work.

so i tested if (string1 isEqualToString:string2) but in that case there is a syntax error

Thanks for any help!

Regards Curtis

Community
  • 1
  • 1
CTSchmidt
  • 1,155
  • 3
  • 17
  • 30

2 Answers2

2

== compares the address of the objects, not their content. Two different objects will obviously never have the same address.

To compare strings use NSString's isEqualToString: method:

if ([string1 isEqualToString:string2]) {
    NSLog(@"it is equal");
}

Note the square brackets [ ]. This is the proper Objective-C syntax for sending messages (i.e. calling functions).

To generally compare object in regards to their content, use isEqual:.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

Use if ([url isEqualToString:turl]). It is the correct syntax, with the [] you mark that you are sending a message to the object.

Dave
  • 1,081
  • 6
  • 10