Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

objective c - comparison of two strings fails

I make a comparison of two strings which are obviously identical. Unfortunately my if clause doesn′t work as expected:

NSLog(@"%@ == %@ ?",strippedString1,strippedString2);

if (strippedString1 == strippedString2) {
    [_newBuild setTextColor: [NSColor greenColor] ]; 
    [_OldBuild setTextColor: [NSColor greenColor] ]; 
}

This is my NSLog output: Build: 2A12-046 == Build: 2A12

Even a byte by byte comparison says both strings are identical:enter image description here

Any clues ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With == you are comparing pointer address, to compare the contents of the strings you could use:

 [strippedString1 isEqualToString: strippedString2];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...