sasi kumar
sasi kumar

Reputation: 753

Comaparing NSString in Objective C

Any one please help me to understand the String comparison technique in Objective-C

NSString *strNew1 = @"AA";
NSString *strNew2 = @"AA";

So to compare both the strings we could use,

   Method 1. if (strNew1 == strNew2) {
          NSLog(@"Equal");
      }

or

Method 2:  if ([strNew1 isEqualToString:strNew2]) {
           NSLog(@"Equal");
       }

In this condition both of them are success. But am aware that method 1 will get failed at certain other condition. And also I have tried the below conditions(All are success).

NSString *strNew = @"AA";
NSString *strNew1 = @"AA";
NSString *strNew11 = [[NSString alloc] initWithString:strNew1];
NSString *strNew3 = strNew;
NSArray *arr = @[@"AA"];
NSString *strNew4 = [arr objectAtIndex:0];
NSString *strNew5 = [arr objectAtIndex:0];
_test = strNew5;
_test1 = @"AA";
if ([strNew isEqualToString:strNew1]) {
    NSLog(@"Equal");
}

if (strNew == strNew3) {
     NSLog(@"Equal1");
}

if (strNew == [arr objectAtIndex:0]){
     NSLog(@"Equal2");
}

if (strNew == strNew4){
    NSLog(@"Equal3");
}

if (strNew5 == strNew4){
    NSLog(@"Equal4");
}

if (strNew4 == [arr objectAtIndex:0]){
    NSLog(@"Equal5");
}

if (strNew11 == [arr objectAtIndex:0]){
    NSLog(@"Equal11");
}

if (self.test == strNew4){
    NSLog(@"Equal3");
}

if (self.test == self.test1){
    NSLog(@"Equal3");
}

TEST *test = [TEST new]; // Tried with a class with NSString property with value "AA" . (test.strTest value is @"AA")
if (strNew == test.strTest) {
   NSLog(@"Equal"); //success
}

I knew most of them are redundant. Am not able to understand the basics behind this. Please anyone give clear explanation on the concept behind this. Thanks.

Upvotes: 0

Views: 581

Answers (4)

Alex Smith
Alex Smith

Reputation: 21

Remember that isEqualToString: comes with a WARNING

[string1 isEqualToString: string2]

will effectively return false is both strings are nil.

Upvotes: 0

Moin Shirazi
Moin Shirazi

Reputation: 4425

The first way compares pointers, while the second way compares objects.

That is, the first way compares if the pointers have the same value. In this case it is likely that they don't, in the second case the objects will be compared. Since they are initialized the same way they could be equal. (Note, it appears that with the UIButton's implementation of isEqual: the result is always false.)

In most cases using == is not what you want. However, what is appropriate depends on your objective.

if (strNew1 == strNew2) //This compared your pointers
    {

    }

and

 if ([strNew1 isEqualToString:strNew2]) //Compares NSString object
    {

    }

Upvotes: 1

UditS
UditS

Reputation: 1956

In the cases you defined the strings created are internally treated as string literals. The runtime will not allocate different memory space to such strings.

Essentially all the strings that contain the same string literal ("AA" in your case) will point to the same memory location. This is done as a part of memory optimization by Apple.

When you change the value of any string (say to "AB") a new address will be allocated to that NSString object and then == will fail.

Upvotes: 2

Vishal Sharma
Vishal Sharma

Reputation: 1733

You need to use below instance method of NSString class.

- (BOOL)isEqualToString:(NSString *)aString;

So, In your case simply follow below:

if ([strNew isEqualToString strNew4]){
    NSLog(@"Equal3");
}

By doing (strNew == strNew4),

You are only comparing the addresses of the objects.

Upvotes: 1

Related Questions