- (void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes使用方法

来源:互联网 发布:cats dogs 数据集 编辑:程序博客网 时间:2024/06/05 17:46
在使用可变数组是我们可以插入单个对象,这个时候只需 给对象和index即可,同时我们也可以插入一个数组,这个时候就需要给一个indexSet而不是index了,

            // Add the first set of elements to the beginning of the array

            for (int i =0; i < [initialData count]; ++i)

            {

                [targetArray insertObject:[initialData objectAtIndex:i] atIndex:i];

            }

        }

        

        - (void)fillInTheBlank:(NSArray *) additions {

              // Start adding at index position 28 and current array has 5 items

这里给出NSIndexSet的创建方法,一个起始点和所要加入的数组的长度即可  。。。。。。

            NSRange range = NSMakeRange(28, [additions count]);     

            NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];

            

            // Can't do this when I'm trying to add 9 new items at index 28

            // because of the error: index 28 beyond bounds [0 .. 5]

            [targetArray insertObjects:additions atIndexes:indexSet];

        }

0 0