Okay I’ve been trying at this for about 2-3 hours now and I don’t seem to quite get it. Here is the code and a brief explanation:
I’m trying to make two lists of words, pull one word from each of those lists at random, and display both words (along with a third) on the screen when a button is pressed. Here is the code:
#import "Project001ViewController.h"
@implementation Project001ViewController
-(ArrayOfWords *) advs
{
if(!advs){
advs = [[ArrayOfWords alloc] init];
NSString* advpath = @"/WordLists/adverbs.txt";
NSLog(@"1");
[[self advs] populateListOfWords:advpath];
}
return advs;
}
-(ArrayOfWords *) adjs
{
if (!adjs) {
adjs = [[ArrayOfWords alloc] init];
NSString* adjpath = @"/WordLists/adjectives.txt";
[[self adjs] populateListOfWords:adjpath];
NSLog(@"2");
}
return adjs;
}
- (IBAction)generate:(UIButton *)sender;
{
//int randy = arc4random() % 11;
//NSNumber* num= [NSNumber numberWithInteger:randy];
NSString* obj = @"app";
NSString* adverb = [[self advs] randomItem];
NSString* adjective = [[self adjs] randomItem];
NSLog(@"%i %i",[adjs size],[advs size]);
NSLog(@"1 %@ %@ %@.",adverb, adjective, obj);
//NSLog(@"%@",thePhrase);
[display setText:@"Hi"];
}
@end
I’m having trouble on the last NSLog line:
NSString* obj = @"app";
NSString* adverb = [[self advs] randomItem];
NSString* adjective = [[self adjs] randomItem];
NSLog(@"%i %i",[adjs size],[advs size]);
NSLog(@"1 %@ %@ %@.",adverb, adjective, obj);
Instead of getting the two randomly selected words (using arc4random() to produce them) the array returns Null. But I know FOR CERTAIN. That the array’s are not empty because the NSLog Line where I print [adjs size] and [advs size] I get the correct sizes of the list of words. I just want to know what is causing them to print Null here.
populateListOfWords, randomItem, and size methods:
- (NSArray *) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:@"\n"];
length=(NSNumber*)([words count]);
return words;
}
-(NSString*) randomItem{
//returns random object in list
return (NSString*)[list objectAtIndex:(arc4random() % (int)length)] ;
}
-(int) size{
//returns size of list
return (int)length;
}
(If more code is needed let me know and thank you in advanced for any and all help).
This method is the problem:
It wasn’t actually putting the words in a list that anyone else could access. I had to just modify it like so:
Now it gives me the correct output. But for some reason when I press the button twice it crashes. Oh well that’s a new problem. Thanks again for all the help.
UPDATE
Turns out
advsandadjswere being released so the second go around it was trying to access a nil value because when I call[self advs] [self adjs]the pointers exist, but their contents do not. I had to go back and refill them each time basically removing theif (!advs)andif (adjs)parts. It now works as intended.