Working on Challenge #2


I don’t want to peek at any of the solutions. I may be doing things the difficult way, I often do! :blush: But I’d like for anyone to take a look at this code snippet and beat it into my brain as to why it doesn’t work.[code]…
NSArray *names = [nameString componentsSeparatedByString:@"\n"];
long nameCount = [names count];

NSLog(@“The names array has ->%@<- entries.”, [NSString stringWithFormat:@"%l", nameCount]);
…[/code]The first line is self-explanatory, just dumping the list of names into the MSArray called names.

The second line I figured out through some searching. It just supplies the count of items in the array. (I’m using a long assuming a short int might not be big enough sometimes and that is the reason for the @"%l". But I don’t think that affects the problem.) Reason I wanted that is to set up a for-loop. Whether that is really the way to run throughout the names is not my question, right now, Mr. Hillegass probably wants us to use the fast enumeration method. But I wanted to do some checking on the data first, so I’m doing it the long (hard?) way. I suppose one could check for a nil to stop the loop, but that seemed both “brute force” and time consuming.

I have managed to see “1309” on the variables side of the Xcode window when my code crashed a few times. That may or may not be the number names, I’m just off on this tangent assuming I might actually learn something! :laughing: BTW, the output of the above code is:The names array has -><- entries.As you can see, the value of nameCount is missing. :unamused:

Thanks for any help! :slight_smile:


For some reason, I was thinking NSLog always needed the “[size=150]%@[/size]” formatting tag as a placeholder for a value. :open_mouth: Thanks to some more searching I find that it does not… always, anyway. Probably because of the way “nameCount” is created/instantiated (is that the correct word?).

I now have the count of the words array outputting the value “1309” with the following:long int nameCount = [names count]; NSLog(@"The names array has -> %ld <- entries.", nameCount);Now, I can start working on the second challenge! :smiley: