Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6171671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:15:29+00:00 2026-05-23T23:15:29+00:00

Okay I’ve been trying at this for about 2-3 hours now and I don’t

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:15:29+00:00Added an answer on May 23, 2026 at 11:15 pm

    This method is the problem:

    - (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;
    
      }
    

    It wasn’t actually putting the words in a list that anyone else could access. I had to just modify it like so:

    - (void) 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"];
    
        list = words;
        length=(int)([words count]);
      }
    

    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 advs and adjs were 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 the if (!advs) and if (adjs) parts. It now works as intended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay,I've been following this tutorial http://coenraets.org/blog/android-samples/androidtutorial/ Basically it gives me exactly what i need
Okay the title cannot explain the situation correct enough. Now this is it, I
Okay so here is what I'm trying to accomplish. First of all below table
Okay, so I have the weirdest problem right now. My code is fine! It
Okay, none of the previous questions I have seen with this error seem to
Okay, so this is a long long shot but here it goes... When IE
Okay so my question is this. Say I have a simple C++ code: #include
Okay, so I'm trying to make a game that uses this algorithm: http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection But
Okay I've spent the last hour trying to find a solution to this but
Okay, this is quite related to my previous quesion , but still (so you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.