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 6177839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:20:49+00:00 2026-05-24T00:20:49+00:00

I have this code to read in a text file, with words separated by

  • 0

I have this code to read in a text file, with words separated by newlines. What I want to do is read all the words into an array and then pick all the six-letter words from that array.

I have this code below, but it seems to be generating an error from within the for loop.

Also, after reading in the text file, do I have to release it?

NSString* path = [[NSBundle mainBundle] pathForResource:@"newdict" ofType:@"txt"];

NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSArray* allLinedStrings = [content componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];

int wordcount = [allLinedStrings count];
int i;
NSMutableArray* sixLetterWords;

for( i = 0 ; i < wordcount ; i++)
{
    NSString* word = [allLinedStrings objectAtIndex: i];
    if (StrLength(word) == 6)
        [sixLetterWords addObject:word];
}
  • 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-24T00:20:50+00:00Added an answer on May 24, 2026 at 12:20 am

    Better options than a for loop are fast enumeration:

    // Don't forget to actually create the mutable array
    NSMutableArray * sixLetterWords = [[NSMutableArray alloc] init];
    for( NSString * word in allLinedStrings ){
        if( [word length] == 6 ) [sixLetterWords addObject:word];
    }    
    

    and blocks-based enumeration with enumerateObjectsUsingBlock::

    NSMutableArray * sixLetterWords = [[NSMutableArray alloc] init];
    [allLinedStrings enumerateObjectsUsingBlock:^(id word, NSUInteger idx, BOOL * stop){
        if( [(NSString *)word length] == 6 ) [sixLetterWords addObject:word];
     }];
    

    There is also the possibility to filter the array:

    NSArray * sixLetterWords = [allLinedStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length == 6"
    

    Note that this last option gives you an autoreleased array — if you want to keep it around, you must retain it. With any of these, you no longer have to worry about the array length or explicit indexing; it is handled for you by the array. Fast enumeration is also, as its name indicates, faster than a plain for loop.

    The method that you used to read the text file into your string, stringWithContentsOfFile:encoding:error:, is not new or alloc, nor does it begin with copy or mutableCopy; therefore, according to Cocoa memory management rules, you do not own it and do not have to release it. (And if you want it to stick around past the end of the current method, you will need to retain it.)

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

Sidebar

Related Questions

I have this code: myVariable which I want to change into trace(myVariable: + myVariable);
I have this problem. I read with BufferedReader text from one system file, this
I have such code to read a text file using BufferedReader : BufferedReader reader=null;
I have this code to sort a text file using arrays in java, but
I usually do not have difficulty to read JavaScript code but for this one
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
I have this code in jQuery, that I want to reimplement with the prototype
I have this code that performs an ajax call and loads the results into
I have this code: public void readTroops() { File file = new File(resources/objects/troops.txt); StringBuffer
I'm trying to convert Morse code into text. I have two text files to

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.