Cy.
Cy.

Reputation: 2145

NSArray element comparison to NSString

I am trying to find a word inside a phrase (NSString).

For this I've exploded the components of the phrase into individual substrings and now I am trying to compare them to the word I am looking for, but it doesn't work.

What would be the correct approach for this and fix for the software below?

NSString *myString = @"Mi Programa es genial";
NSArray *explodedDescription = [myString componentsSeparatedByString:@" "];

if ([explodedDescription objectAtIndex:1] == @"Programa" ) {
    NSLog(@"Found");
}

Upvotes: 0

Views: 1192

Answers (2)

user155959
user155959

Reputation:

You're doing it the hard way! Use NSString's -rangeOfString: method or one of its variants to get the location of a string in another string.

Upvotes: 2

diciu
diciu

Reputation: 29333

NSStrings are compared with isEqualToString. You're comparing pointers instead of the values.

See NSString documentation

Upvotes: 7

Related Questions