I have an array of names but can’t seem to make the comparison work. Do I have an improper use of the language here?
NSLog(@"%@",[arrayOfNames objectAtIndex:0]);
if ([arrayOfNames objectAtIndex:0] == "Blue"){
NSLog(@"it's Blue");
}
else {
NSLog(@"it's not Blue");
}
The output is the following one:
Blue
it’s not Blue
Use the following:
You’re comparing two objects (one of the id-type, the other is a C-string) with the == operator. The comparison will fail, since they are 2 different objects. With the isEqualToString you are comparing the value of the object to the string
@"Blue".