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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:31:15+00:00 2026-06-13T11:31:15+00:00

I create an array based on a dictionaries key’s: factsBuiltArray = [NSMutableArray arrayWithCapacity: 6];

  • 0

I create an array based on a dictionaries key’s:

factsBuiltArray = [NSMutableArray arrayWithCapacity: 6];
if ([statusDict count] == 10) {
    for (NSString *key in [statusDict allKeys]) {
        if ([key isEqualToString: @"currenciesAndConversions"]) {
            [factsBuiltArray insertObject:key atIndex: 0];
        }
        else if ([key isEqualToString: @"languageAndTranslations"]) {
            [factsBuiltArray insertObject:key atIndex: 1];
        }
        else if ([key isEqualToString: @"plugSize"]) {
            [factsBuiltArray insertObject:key atIndex: 2];
        }
        else  if ([key isEqualToString: @"timezone"]) {
            [factsBuiltArray insertObject:key atIndex: 3]; // crashes over here
        }
        else if ([key isEqualToString: @"population"]) {
            [factsBuiltArray insertObject:key atIndex: 4];
        }
        else if ([key isEqualToString: @"wikipedia"]) {
            [factsBuiltArray insertObject:key atIndex: 5];
        }
    }
}

The crash log is:

*** -[__NSArrayM insertObject:atIndex:]: index 3 beyond bounds for empty array

Why does inserting an object to an array that is specified with a capacity of 6 make it crash? Very confusing!

  • 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-13T11:31:16+00:00Added an answer on June 13, 2026 at 11:31 am

    The capacity is merely how many objects a container class can hold. Inserting at an index requires that index to be a valid placement for the new object based on the total number of objects contained in the container (not the total number of objects that CAN be contained).

    If your array’s values are index dependent (which is seems like perhaps a different architecture or data structure would be better) then you can ensure that every index is filled by prepopulating the array with NSNulls. This would require you to check for NSNulls when reading from the array later on though which would likely be extra work, hence why this is probably not the best approach. In any case, you can change your code to the following to fix your crash.

    factsBuiltArray = [NSMutableArray arrayWithCapacity: 6];
    
    for (NSUInter i = 0; i < 6; i++) {
        [factsBuiltArray addObject:[NSNull null]];
    }
    
    if ([statusDict count] == 10) {
    
        for (NSString *key in [statusDict allKeys]) {
    
            if ([key isEqualToString: @"currenciesAndConversions"]) {
    
                [factsBuiltArray replaceObjectAtIndex:0 withObject:key];
            }
            else if ([key isEqualToString: @"languageAndTranslations"]) {
    
                [factsBuiltArray replaceObjectAtIndex:1 withObject:key];
            }
            else if ([key isEqualToString: @"plugSize"]) {
    
                [factsBuiltArray replaceObjectAtIndex:2 withObject:key];
            }
            else  if ([key isEqualToString: @"timezone"]) {
    
            [factsBuiltArray replaceObjectAtIndex:3 withObject:key];
    
            }
            else if ([key isEqualToString: @"population"]) {
    
                [factsBuiltArray replaceObjectAtIndex:4 withObject:key];
            }
            else if ([key isEqualToString: @"wikipedia"]) {
    
                [factsBuiltArray replaceObjectAtIndex:5 withObject:key];
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I create and initialize an array in F# based on a given
I am looking for a way to create a new array based on array
I want to create a subarray from an array based on pattern match. for
I need to dynamically create an array based on a range. I have a
I want to create a multidimensional array based on a string. the string has
I am trying to create an array based implementation of Stacks. I am a
I want to dynamic create an array based on a number inside a multidimensional
I would like to create a new array B_array based on an existing array
I am trying to create a 2d array class based on boost::multi_array . I
I create array: TextBox[] textarray = new TextBox[100]; Then in cycle set this params,

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.