简体   繁体   English

NSMutableArray insertObject:atIndex的异常行为

[英]Unexpected behavior from NSMutableArray insertObject:atIndex

i try to make an array that has the numbers from 1 to 30 using.. 我试图使数组具有从1到30的数字。

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: 我有一个包含35个元素的数组,它们的值为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: 现在,我要使用以下命令将numberOfDaysInAMonth中的值插入到index:1中的LatestArrayWithMonth statrting中:

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? 为什么我的输出全都是2?

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. 因为您要连续插入atIndex:1项,所以随着循环中'i'变量的增加,数组中的项数也增加了-因此索引'i'处的项记录始终是相同的项。

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. 还请检查是否要在索引1处插入对象。Objective-C是零索引语言,这意味着数组中的第一项位于索引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). 您可能想要删除latestArrayForMonth数组中的所有对象,而只需使用addObject方法(或使用addObjectsFromArray快速添加数组中的所有项目)。

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). 需要注意的另一件事是,由于您仅存储原始整数值,因此您可能会发现NSIndexSet比NSArray更适合使用(取决于所发布代码的应用程序,但值得研究)。

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. 使用insertObject:... atIndex:1您要在索引1插入第1个月(没有元素,因此它会进入索引0),然后是第2个月(实际上是在索引1),然后继续在2之前插入。在5个元素之后数组看起来像这样的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. 您应该做的就是通过从索引中减去1来访问数组,但是如果要访问从索引1开始的数组,请首先添加一个NSNull。

[latestArrayForMonth addObject:[NSNull null]];

for (i = 0; i < 30; i++){

    [latestArrayForMonth addObject:[numberOfdaysInAMonth objectAtIndex:i]];

    NSLog (@"Element_latestArrayForMonth %d = %@", i + 1, [latestArrayForMonth objectAtIndex: i + 1]);  

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM