Trying to read a list of random adjectives for fun, but


#1

I’ve done this before in Objective-C (with the question app from chapter one) where I want to read in a text file as a string, then read each line as a NSString into the NSArray. This is the code I’m using in the class method randomItem…

[code] NSString *nounPath = [[NSBundle mainBundle] pathForResource:@“nounlist” ofType:@“txt”];

NSString *nounString = [NSString stringWithContentsOfFile:nounPath encoding:NSUTF8StringEncoding error:nil];

NSLog(@"Noun String: %@", nounString);

randomNounList = [nounString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

NSString *adjectiveString = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"adjectivelist" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"Adjective String: %@", adjectiveString);

randomAdjectiveList = [adjectiveString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];[/code]

The output for the two NSLog’s is (null). So what is going wrong here? I made sure that the two text files are UTF8 and also they are in the target membership. Does this have something to do with it being a command-line project? The two NSArrays were declared in BNRItem.h before @interface, but that doesn’t seem to be the issue since the NSString nounPath isn’t even being created properly.

I’ve been fiddling around with this for a couple hours now, and I know it’s pretty meaningless, but I can’t stop now!

Thanks!


#2

Because you have created a Command Line Tool application, this will not have an application bundle like OS X and iOS applications.

Therefore, you will need to use NSFileManager instead, and place your file(s) in the current working directory which you can find out by using:

Hope this helps,
Nick

http://myfirstiphoneapp.co.uk
http://easyintervalsapp.co.uk


#3

That did the trick. Thanks Nick!