Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison in Objective-C

I've currently got a webserver set up which I communicate over SOAP with my iPhone app. I am returning a NSString containing a GUID and when I attempt to compare this with another NSString I get some strange results.

Why would this not fire? Surely the two strings are a match?

NSString *myString = @"hello world";  if (myString == @"hello world")     return; 
like image 669
ingh.am Avatar asked Apr 07 '10 12:04

ingh.am


People also ask

How to compare two strings in objective C?

To compare two strings equality, use isEqualToString: . BOOL result = [firstString isEqualToString:secondString]; To compare with the empty string ( @"" ), better use length .

How do I check if a string contains a substring in Objective C?

To check if a string contains another string in objective-c, we can use the rangeOfString: instance method where it returns the {NSNotFound, 0} if a 'searchString' is not found or empty (""). Output: string contains you!

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

Is equal to string Swift?

To check if two strings are equal in Swift, use equal to operator == with the two strings as operands. The equal to operator returns true if both the strings are equal or false if the strings are not equal.


1 Answers

Use the -isEqualToString: method to compare the value of two strings. Using the C == operator will simply compare the addresses of the objects.

if ([category isEqualToString:@"Some String"]) {     // Do stuff... } 
like image 113
jlehr Avatar answered Sep 21 '22 18:09

jlehr