Anshuman Mishra
Anshuman Mishra

Reputation: 189

how i will compare string value with a nsmutablearray

if ([pArray objectAtIndex:2]==@"ROOT")
{
    NSLog(@"YES");
}
else {
    NSLog(@"NO");
}

I am using this code but it's not working

Upvotes: 0

Views: 915

Answers (5)

Radu
Radu

Reputation: 3494

        if (([pArray objectAtIndex:2]  != (id)[NSNull null])&([[pArray objectAtIndex:2] isEqual:@"ROOT"])){
        NSLog(@"String in array is %@",[pArray objectAtIndex:2]);
    }
    else {
       if([pArray objectAtIndex:2]  == (id)[NSNull null]){
        NSLog(@"Your Object is Null");
       }
else{
   NSLog(@"Your Object does not match");
}   
}

Upvotes: 0

Himanshu A Jadav
Himanshu A Jadav

Reputation: 2306

You can use

- (BOOL)containsObject:(id)anObject; 

to check if the object is in the array. eg. [pArray containsObject:@"ROOT"];

if you need to check if particular element is equal to a string you can use.

[[pArray objectAtIndex:2] isEqualToString:@"ROOT"];

Both will return YES if matched.

Upvotes: 1

Steve Rukuts
Steve Rukuts

Reputation: 9367

You must use the isEqualToString method. You are attempting to compare two references in your statement.

if ([[pArray objectAtIndex:2] isEqualToString:@"ROOT"])

Upvotes: 0

Simon Lee
Simon Lee

Reputation: 22334

Assuming pArray contains strings... use the following...

if([[pArray objectAtIndex:2] isEqualToString:@"ROOT"])

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

Use isEqualToString: method:

if ([[pArray objectAtIndex:2] isEqualToString:@"ROOT"]){
...
}

Upvotes: 0

Related Questions