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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:54:55+00:00 2026-06-02T00:54:55+00:00

I am working on a word-based game, and I need to load my 180,000

  • 0

I am working on a word-based game, and I need to load my 180,000 word dictionary (1.9MB) into an array so the solving algorithm can work with it. The dictionary is simply one word per line, like this:

a
ab
abs
absolutely
etc
...

Right now I am using the following code to load that file into the array:

NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"txt"];
        NSString *stringFromFile = [[NSString alloc]
                                    initWithContentsOfFile:txtPath
                                    encoding:NSUTF8StringEncoding
                                    error:&error ];       
for (NSString *word in [stringFromFile componentsSeparatedByString:@"\r\n"]) {
            [wordsArray addObject:word];
        } 

This takes about 3-4 seconds on an iPhone 4. Probably even slower on older iOS devices. Is there a faster way to do this?

  • 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-06-02T00:54:56+00:00Added an answer on June 2, 2026 at 12:54 am

    You can easily do this in the background, off the main thread using Grand Central Dispatch, or GCD.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),^(void){
            NSString *txtPath = [[NSBundle mainBundle] pathForResource:@"dict" ofType:@"txt"];
            NSString *stringFromFile = [[NSString alloc]
                                        initWithContentsOfFile:txtPath
                                        encoding:NSUTF8StringEncoding
                                        error:&error ];       
            for (NSString *word in [stringFromFile componentsSeparatedByString:@"\r\n"]) {
                [wordsArray addObject:word];
            } 
    });
    

    I wrote the enclosing dispatch code from memory, but it’s close (if not correct) and I think you get the idea.

    EDIT: You can execute this code on the main thread, but what happens is a non-main-thread dispatch queue is where your code executes, thereby NOT blocking the UI.

    You can improve the performance of your code here a little bit by replacing the for loop with just this:

    [wordsArray setArray:[stringFromFile componentsSeparatedByString:@"\r\n"]];
    

    There should be no need to iterate over the result from -componentsSeparatedByString: (an array) just to put them into another array. With 180K words, that should be a significant time saving right there.

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

Sidebar

Related Questions

I am working on a small project which involves a dictionary based text searching
I am working on a system that can create made up fanatsy words based
I am working on prototypes to replace an existing word-automation based system for template
I'm trying to make a QML-based dictionary application. It fetches the word definition via
I'm working on a word-press theme and whenever I add the code below to,
I am working on toolbar of word processor. Usually when we click on any
Working a on a Google Web Toolkit project. I want to show a word
Working on a small game using an HTML5 canvas, and javascript. And it's working
Working with an undisclosed API, I found a function that can set the number
I'm making an Android App for my friends and family based on the game

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.