2nd Challenge Confusion


#1

Hey guys,

I’m working through the second challenge of this section of the book, and I read a few other topics on this very subject. My confusion arises when I compare my code to successful code. Mine should work, but it isn’t. Can you guys spot anything I might have missed?

[code]#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
  //Read in a file as a huge string (ignoring the possiblity of an error)
    NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propername"
                                                     encoding:NSUTF8StringEncoding
                                                        error:NULL];
    //Break it into an array of strings
    NSArray *names = [nameString componentsSeparatedByString:@"\n"];
    NSArray *words = [wordString componentsSeparatedByString:@"\n"];
    //Go through the arrays inefficiently one string at a time
    for (NSString *n in names)
    {
        //look for strings that are both names and things in a case-insensitive manner
        for (NSString *x in words)
        {
            if ([n caseInsensitiveCompare:x] == NSOrderedSame)
                NSLog(@"%@ and %@ are considered equal", x, n);
        }
       
    }
    
    
    
}
return 0;

}
[/code]


#2

[color=#FF0000]propername[/color] should be pluralized as propernames.

You could have easily discovered this yourself by using the cave-man-debugging technique:

#import <Foundation/Foundation.h>
    
int main (int argc, const char * argv[])
{
    @autoreleasepool {
        
        //Read in a file as a huge string (ignoring the possiblity of an error)
        NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                                         encoding:NSUTF8StringEncoding
                                                            error:NULL];
        NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propername"
                                                         encoding:NSUTF8StringEncoding
                                                            error:NULL];

        //Break it into an array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];
        
        NSLog (@"names {%@}", names);
        NSLog (@"words {%@}", words);

        //Go through the arrays inefficiently one string at a time
        for (NSString *n in names)
        {
            //look for strings that are both names and things in a case-insensitive manner
            for (NSString *x in words)
            {
                NSLog (@"%@ %@", n, x);
                if ([n caseInsensitiveCompare:x] == NSOrderedSame)
                    NSLog (@"%@ and %@ are considered equal", x, n);
            }
            
        }
    }
    return 0;
}

You would have seen words {(null)} in the output:

2012-07-20 09:50:35.531 Confused[54930:503] names {(
    A,
    a,
    aa,
...
    zythum,
    Zyzomys,
    Zyzzogeton,
    ""
)}
2012-07-20 09:50:37.071 Confused[54930:503] words {(null)}

Then you would have gone and investigate :slight_smile:


#3

Do you have your strings and files reversed? You are loading the words file into nameString and the file propernames into wordString.

NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words" encoding:NSUTF8StringEncoding error:NULL]; NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propername" encoding:NSUTF8StringEncoding error:NULL];
Mitch


#4

Hey guys,

Thanks for the responses! I’ve never heard of the caveman-debugging-technique but I’m assuming it just means it’s simple and I should have seen it?

Also, I think the string names were mixed up, thanks for catching that. I’m still working on the logic of differentiation between exactly similar names, IE Yvonne and Yvonne.

This is fun @_@.


#5

Where is this file coming from (/usr/share/dict/propernames)? I searched for this file in my spotlight search and finder and it doesn’t return anything. What am I missing? Sorry for the ignorance, but I am still getting used to apples file system…How can I find the file on my machine? And, if I didn’t download any resources from this book, how does this file exist on my computer?