Create a new Foundation Command Line Tool named Groceries. Start by creating an empty NSMutableArray object. Then add several grocery-like items to the array. (You’ll have to create those, too.) Finally, use fast enumeration to print out your grocery list.
This next challenge is, well, more challenging. Read through the following program, which finds common proper names that contain two adjacent A’s.
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { // Read in a file as a huge string (ignoring the possibility of an error) NSString *nameString = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames" encoding:NSUTF8StringEncoding error:NULL]; // Break it into an array of strings NSArray *names = [nameString componentsSeparatedByString:@"n"]; // Go through the array one string at a time for (NSString *n in names) { // Look for the string "aa" in a case-insensitive manner NSRange r = [n rangeOfString:@"AA" options:NSCaseInsensitiveSearch]; // Was it found? if (r.location != NSNotFound) { NSLog(@"%@", n); } } } return 0; }
The file /usr/share/dict/propernames contains common proper names. The file/usr/share/dict/words contains regular words (not proper names). Now write a program based on the one above that finds common proper names that are also regular words. For example, “Glen” is a guy’s name, and “glen” is a narrow valley.
When a computer orders strings, it typically considers uppercase letters as coming before lowercase letters. To do a comparison that ignores the case, use the method caseInsensitiveCompare:.
NSString *a = @"ABC"; NSString *b = @"abc"; if ([a caseInsensitiveCompare:b] == NSOrderedSame) { NSLog(@"a and b are equal"); } if ([a caseInsensitiveCompare:b] == NSOrderedAscending) { NSLog(@"a comes before b"); } if ([a caseInsensitiveCompare:b] == NSOrderedDescending) { NSLog(@"b comes before a"); }
In this challenge, use caseInsensitiveCompare: to see that “Glen” and “glen” are equivalent.