0

I've been learning Objective-C for five days and I have only 2 weeks of prior programming experience so please make answers as simple as possible.

I'm doing an exercise in a book that asks me to generate a list of proper names that are also regular words. To do this I have am running a for loop for each proper name from a NSArray proper name object. Within that for loop I have a nested for loop testing each name against each word in an NSArray regular word object using the caseInsensitiveCompare method.

Here is my code:

import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {

        //Gets the sting with proper names
        NSString *propername = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:
        NSUTF8StringEncoding error:NULL];

        //Gets the string with regularwords
        NSString *inpropername = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:
                                NSUTF8StringEncoding error:NULL];

        NSArray *proper = [propername componentsSeparatedByString:@"/n"];
        NSArray *inproper = [inpropername componentsSeparatedByString:@"/n"];

        for (NSString *n in proper){
            NSLog(@"%@", n);
            for(NSString *i in inproper){
                NSLog(@"%@", i);
                if ([n caseInsensitiveCompare:i] == NSOrderedSame)
                {
                    NSLog(@"Yahooo! Got One! %@", n);
                }
            }
        }

    }
    return 0;
}

Instead of the for loops running in a nested fashion they are running in a sequential fashion. The output is like this:

Aaron  
all the names...  
Yvonne  
a  
all the regular words....  
Zyzzogeton  

Any ideas for why the nested for loop is not running in a nested fashion?

5
  • My guess is that you're not showing us something. Commented Aug 12, 2012 at 1:44
  • 3
    The "/n" is suspicious… I guess you meant "\n" Commented Aug 12, 2012 at 2:03
  • You clearly haven't added all your code, as nowhere in the code shown do you print "all the names..." or "all the regular words...". Commented Aug 12, 2012 at 2:04
  • "all the names" is just a way to say that "here I see all the names", it's not part of what is actually displayed. :-) Commented Aug 12, 2012 at 2:07
  • Julien you were right - it should have been "\n", because of that each object only contained one big long string. That's one problem solved. Commented Aug 12, 2012 at 2:13

1 Answer 1

4

The code is correct except you are not breaking the files into words since you are using "/n" instead of "\n".

This means that each array contains exactly one element which is a string with all the words in it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.