Unexpected behavior from NSMutableArray insertObject:atIndex?

Up vote 1 down vote favorite share g+ share fb share tw.

I try to make an array that has the numbers from 1 to 30 using.. NSMutableArray *numberOfdaysInAMonth = NSMutableArray alloc init ; for ( i=0; I 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? Iphone objective-c nsmutablearray link|improve this question edited Feb 25 at 17:11yuji5,9422822 asked Jun 23 '11 at 21:05user80528882.

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

Using insertObject:... atIndex:1 you are inserting month 1 at index 1 (no elements so it goes to index 0), then month 2 (which is actually at index 1) then you keep inserting before 2. After 5 elements your array will look like this 1,5,4,3,2. What you should do is just access the array by subtracting 1 from the index, but if you want to access starting at index 1 add an NSNull first.

LatestArrayForMonth addObject:NSNull null; for (i = 0; I.

Using insertObject:... atIndex:1 you are inserting month 1 at index 1 (no elements so it goes to index 0), then month 2 (which is actually at index 1) then you keep inserting before 2. After 5 elements your array will look like this 1,5,4,3,2. What you should do is just access the array by subtracting 1 from the index, but if you want to access starting at index 1 add an NSNull first.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions