Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

If statement not finding string

I have a problem with the following code:

myString = myTextField.text;

    for (int i = 0; i < [array1 count]; i++) {

        if (myString == array1[i]) {
            myString = array2[i];
        }
            }

    NSLog(myString);

This does not log anything however if I change "myString" to the code below, it does work. Any ideas?

myString = @"1";

    for (int i = 0; i < [array1 count]; i++) {

        if (myString == array1[i]) {
            myString = array2[i];
        }
            }

    NSLog(myString);

2 Answers

Try using isEqualToString: instead of == for your string comparison.
so:

if ([myString isEqualToString: array1[i]]) {

That worked. It makes the change but once it has made the on the first word in the string however it does not continue to check the others. Do you know why this could be?

Maybe try changing the NSLog statement to this:

NSLog(@"%@", myString);