Hi guys,
I'm re-learning my Objective-C (got my book back
) and I'm stuck on this one exercise. I can recreate it easy enough in C++ but not in Objective-C.
What its supposed to do is find every occurrence of a word in the proper names list found in /usr/share/dict/propernames that is also found in /usr/share/dict/words.
The example the book gives is "Glen" and "glen" since Glen is a persons name and also a noun.
I have tried all sorts of stuff and I keep getting weird results. I already checked to ensure that the arrays holding the proper names list and the words list are correct, but the comparisons are not working correctly and I'm not sure why.
Can someone with fresh eyes spot what I am doing wrong? Does NSOrderedSame not work how I think it works?
EDIT: I'm not sure if I found the problem or not, but it appears that the dictionary list contains proper names as well! It could have been correct the entire time 0_0! But I'll just leave it incase someone else can spot something wrong.
EDIT2: Oops I forgot my other comment. The results should be names like "Glen and glen" or "Brook" and brook" but everything is showing up so I'm pretty sure its because the common names are now in the dictionary list. I see Aaron is in the dictionary list.
I'm re-learning my Objective-C (got my book back
What its supposed to do is find every occurrence of a word in the proper names list found in /usr/share/dict/propernames that is also found in /usr/share/dict/words.
The example the book gives is "Glen" and "glen" since Glen is a persons name and also a noun.
I have tried all sorts of stuff and I keep getting weird results. I already checked to ensure that the arrays holding the proper names list and the words list are correct, but the comparisons are not working correctly and I'm not sure why.
Can someone with fresh eyes spot what I am doing wrong? Does NSOrderedSame not work how I think it works?
Code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
@autoreleasepool {
//Read in the file as a string
NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
encoding:NSUTF8StringEncoding
error:NULL];
NSString *wordString = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
encoding:NSUTF8StringEncoding
error: NULL];
//Break it into an array of strings
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
NSArray *words = [wordString componentsSeparatedByString:@"\n"];
//This is where things go wrong
for(NSString *w in words)
{
for(NSString *n in names)
{
if([w caseInsensitiveCompare:n] == NSOrderedSame)
{
NSLog(@"%@ = w, %@ = n", w, n); //This comparison produces weird results!
}
}
}
}
return 0;
}
EDIT: I'm not sure if I found the problem or not, but it appears that the dictionary list contains proper names as well! It could have been correct the entire time 0_0! But I'll just leave it incase someone else can spot something wrong.
EDIT2: Oops I forgot my other comment. The results should be names like "Glen and glen" or "Brook" and brook" but everything is showing up so I'm pretty sure its because the common names are now in the dictionary list. I see Aaron is in the dictionary list.
Last edited: