2 questions: NSArray - instances from diff classes + Chal 2


#1

Hi all,

I have 2 questions for this chapter.

Since an NSArray is an array of pointers can the pointers be to instances of different classes. I guess the answer is yes but would like to hear it here.

Also, for Challenge 2 I used a nested loop:

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // Read in file one as a huge string (ignore possibility of error)
        NSString *nameString
        = [NSString stringWithContentsOfFile:@"/usr/share/dict/propernames"
                                    encoding:NSUTF8StringEncoding 
                                       error:NULL];
        // Read in file two as a huge string (ignore possibility of error)
        NSString *wordString
        = [NSString stringWithContentsOfFile:@"/usr/share/dict/words"
                                    encoding:NSUTF8StringEncoding
                                       error:NULL];
        
        // Break file one into an array of strings
        NSArray *names = [nameString componentsSeparatedByString:@"\n"];
        
        // Break file two into an array of strings
        NSArray *words = [wordString componentsSeparatedByString:@"\n"];
        
        // Loop through the name array one string at a time
        
        for (NSString *n in names) {
            
            // Loop through the word array one string at a time
            for (NSString *w in words) {
                
                // compare the elements and log if same
                if ([n caseInsensitiveCompare:w] == NSOrderedSame) {
                    NSLog(@"%@ = %@", n, w);
                }
            }
        }
        
        
    }
    return 0;
}

and at the end whitespace is matched:

2012-02-06 17:55:49.290 Challenge2b[876:707] Aaron = Aaron
2012-02-06 17:55:49.310 Challenge2b[876:707] Adam = Adam
2012-02-06 17:55:49.329 Challenge2b[876:707] Adlai = Adlai

...

2012-02-06 17:56:19.033 Challenge2b[876:707] Woody = Woody
2012-02-06 17:56:19.034 Challenge2b[876:707] Woody = woody
2012-02-06 17:56:19.052 Challenge2b[876:707] Yvonne = Yvonne
2012-02-06 17:56:19.075 Challenge2b[876:707]  = 

Is this due to the EOF character?

Thanks,
Frank


#2

Question 1: Yes, an array can hold instances from an assortment of classes.


#3

Question 2: I think there is an extra newline character at the end of each of the files.