简体   繁体   English

NSMutableArray insertObject不起作用

[英]NSMutableArray insertObject don't work

i try to insert data in my NSMutableArray : 我尝试在NSMutableArray中插入数据:

this is a part of my code: 这是我的代码的一部分:

 newArray=[[NSMutableArray alloc]init];

 fileArray = [[NSMutableArray alloc] initWithContentsOfFile: file];
            [newArray insertObject:dict atIndex:[fileArray count] ];

it's crash with this error: 它因以下错误而崩溃:

[__NSArrayM insertObject:atIndex:]: index 11 beyond bounds for empty array'

thanks for reading 谢谢阅读

  1. NSMutalbeArray: insertObject:atIndex: method, the index value must not be greater than the count of elements in the array. NSMutalbeArray:insertObject:atIndex:方法,索引值不得大于数组中元素的数量。

  2. NSMutalbeArray: initWithCapacity: method, even though specify a size when create the array, the specified size is regarded as a “hint”; NSMutalbeArray:initWithCapacity:方法,即使在创建数组时指定大小,指定的大小也被视为“提示”。 the actual size of the array is still 0. 数组的实际大小仍为0。

  3. The only method you can do to insertObject:atIndex, is to ensure to initialize all the elements before the index value. 您可以执行insertObject:atIndex的唯一方法是确保初始化索引值之前的所有元素。

     for(int i = 0; i< index; i++) { [array addObject:[NSNull null]]; } 

This should work: 这应该工作:

newArray=[[NSMutableArray alloc]init];
fileArray = [[NSMutableArray alloc] initWithContentsOfFile: file];
[newArray addObject:dict];

First of all, as far as I know, when you call: 据我所知,首先,当您致电:

 NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 20]

It will create and return an NSMutableArray object with enough memory to initially hold a given number of objects. 它将创建并返回一个NSMutableArray对象,该对象具有足够的内存来最初容纳给定数量的对象。 This is not a number of objects itself. 这本身不是许多对象。

So, ( probably this isn't a good way ), you can fill your array with some null-objects like this: 因此,( 可能这不是一个好方法 ),您可以使用一些空对象来填充数组,如下所示:

for (NSInteger i = 0; i < 20; i++)
{
    [array addObject:[NSNull null]];
}

And now you can use replaceObectAtIndex:WithObject: method for your purposes. 现在,您可以根据需要使用replaceObectAtIndex:WithObject:方法。 Because I guess this is what you want ( insertObject:AtIndex: — means that you are going to insert an object at some index, but the rest part of the array (after that index) will "move", and there will be array.count+1 items now). 因为我猜想这就是您想要的( insertObject:AtIndex: —意味着您将在某个索引处插入对象,但是数组的其余部分(在该索引之后)将“移动”,并且将存在数组。现在计数+1个项目)。

[array replaceObjectAtIndex:someIndex withObject:someObject];

You are trying to insert an object at an index that is greater then the array count. 您正在尝试在比数组数大的索引处插入对象。

This is because you are inserting the object into newArray, which has a count of 0 when you initialize it. 这是因为要将对象插入到newArray中,该对象在初始化时的计数为0。

If you are really wanting to add the object to newArray, use index 0. 如果您确实想将对象添加到newArray,请使用索引0。

You can't add an object at a specific index if that index doesn't exist, and the way you're creating your array creates one with a length of 0. 如果该索引不存在,则无法在该索引处添加对象,并且创建数组的方式会创建一个长度为0的对象。

You can either predefine the size of the array like this: 您可以像这样预先定义数组的大小:

NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:20];
[newArray insertObject:object atIndex:index];

Or you can just insert objects from the beginning and let the array grow from 0, like this: 或者,您可以只从头开始插入对象,然后让数组从0开始增长,如下所示:

NSMutableArray *newArray = [[NSMutableArray alloc] init];
[newArray addObject:object];

Which will put it at index 0 if it's the first time you add an object, at index 1 the next time, etc. 如果是第一次添加对象,则将其放置在索引0处,下次将其放置在索引1处,依此类推。

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

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