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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:23:49+00:00 2026-05-23T10:23:49+00:00

i try to make an array that has the numbers from 1 to 30

  • 0

i try to make an array that has the numbers from 1 to 30 using..

NSMutableArray *numberOfdaysInAMonth = [[NSMutableArray alloc] init ] ;

for ( i=0; i< 30; i++){
    [numberOfdaysInAMonth  addObject:[NSNumber numberWithInteger:i+1]];
    NSLog (@"Element_numberofDaysinAMonth %i = %@", i, [numberOfdaysInAMonth objectAtIndex: i]); 
}

and I have an array with 35 elements and they have the value of null:

NSMutableArray *latestArrayForMonth = [[NSMutableArray alloc]init ];

for ( i=0; i<36; i++){        
    [latestArrayForMonth addObject:[NSNull null]];         
}

Now I want to insert the values from numberOfDaysInAMonth into the LatestArrayWithMonth statrting from index:1 using:

for (i = 0; i < 30; i++){  
    [latestArrayForMonth insertObject:[numberOfdaysInAMonth objectAtIndex:i] atIndex:1];
    NSLog (@"Element_latestArrayForMonth %i = %@", i, [latestArrayForMonth objectAtIndex: i]);  
}

Output on the Console is:

Element_latestArrayForMonth 0 = <null>
Element_latestArrayForMonth 1 = 2
Element_latestArrayForMonth 2 = 2
Element_latestArrayForMonth 3 = 2
Element_latestArrayForMonth 4 = 2
Element_latestArrayForMonth 5 = 2
.
.
.
Element_latestArrayForMonth 28 = 2
Element_latestArrayForMonth 29 = 2

Why am I getting all 2s on the output?

  • 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-23T10:23:50+00:00Added an answer on May 23, 2026 at 10:23 am

    It’s because of this line:

    [latestArrayForMonth insertObject:[numberOfdaysInAMonth objectAtIndex:i] atIndex:1];
    

    Because you’re inserting the item atIndex:1 continuously, as the ‘i’ variable is increasing in the loop, so is the number of items in the array – so the logging of the item at index ‘i’ is always the same item.

    Please also check that inserting the object at index 1 is what you are wanting to do – Objective-C is a zero-indexed language, meaning that the first item in the array is at index 0.

    You probably want to remove all the objects in the latestArrayForMonth array and simply use the addObject method (or use addObjectsFromArray to quickly add all the items from the array).

    Also might be worthy investigating alternative approaches to having an array full of ’empty’ values – I’ve never seen a use case for this approach yet, better to have an empty array than an array full of ’empty’ values.

    One other thing to note is that since you’re just storing primitive integer values, you may find NSIndexSet more applicable to use than NSArray (depends on the application of the posted code, but worth investigation).

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

Sidebar

Related Questions

When I try to make a very large boolean array using Java, such as:
I'll try to make this as straight forward as possible. Currently our team has
Whenever I use WCF, I always try to make immutable classes that end up
I'm trying to make a program in Visual C# that has my one created
Tried using the following: Populate Listview from JSON To make a listview which uses
I try to make a registration form. When user post the fields, i check
I will try to make this as clear as I can, but if you
I'm looking to try to make an efficient delete method to work on a
I am starting this question to try and make a central point developers can
What will be the code when I try to make a combo box read

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.