Instance Method インスタンスメソッド

insertObjects:atIndexes:

Inserts the objects in the provided array into the receiving array at the specified indexes. 提供された配列の中のオブジェクトそれらを受け手側の配列へとその指定されたインデックスそれらで挿入します。

Declaration 宣言

- (void)insertObjects:(NSArray<ObjectType> *)objects 
            atIndexes:(NSIndexSet *)indexes;

Parameters パラメータ

objects

An array of objects to insert into the receiving array. 受け手側の配列へと挿入されることになるオブジェクトいくらかからなる配列。

indexes

The indexes at which the objects in objects should be inserted. The count of locations in indexes must equal the count of objects. For more details, see the Discussion. それでobjectsの中のオブジェクトが挿入されるべきインデックスいくらか。indexesの中の位置の総数は、objectsの総数と等しくなければなりません。詳細として、議論を見てください。

Discussion 議論

Each object in objects is inserted into the receiving array in turn at the corresponding location specified in indexes after earlier insertions have been made. The implementation is conceptually similar to that illustrated in the following example: objectsの中の各オブジェクトは、受け手側の配列へと順に、indexesにおいて指定される対応する位置で、より前の挿入がなされてしまった後に挿入されます。実装は、概念的に以下の例で説明されるものと似ています:


- void insertObjects:(NSArray *)additions atIndexes:(NSIndexSet *)indexes
{
    NSUInteger currentIndex = [indexes firstIndex];
    NSUInteger i, count = [indexes count];
 
    for (i = 0; i < count; i++)
    {
        [self insertObject:[additions objectAtIndex:i] atIndex:currentIndex];
        currentIndex = [indexes indexGreaterThanIndex:currentIndex];
    }
}

The resulting behavior is illustrated by the following example: 結果の挙動は、以下の例で図解されます:


NSMutableArray *array = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil];
NSArray *newAdditions = [NSArray arrayWithObjects: @"a", @"b", nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
[indexes addIndex:3];
[array insertObjects:newAdditions atIndexes:indexes];
NSLog(@"array: %@", array);
 
// Output: array: (one, a, two, b, three, four)

The locations specified by indexes may therefore only exceed the bounds of the receiving array if one location specifies the count of the array or the count of the array after preceding insertions, and other locations exceeding the bounds do so in a contiguous fashion from that location, as illustrated in the following examples. indexesによって指定される位置が、したがって受け手側の配列の境界を唯一越えるかもしれないのは、ある位置が配列の総数または先行する挿入の後の配列の総数を指定する、そして境界を越えているその他の位置がその位置から隣り合う風にそうする場合です、以下の例で説明されるように。

In this example, both new objects are appended to the end of the array. この例において、新しいオブジェクトは両方とも配列の終わりに加えられます。


NSMutableArray *array = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil];
NSArray *newAdditions = [NSArray arrayWithObjects: @"a", @"b", nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:5];
[indexes addIndex:4];
[array insertObjects:newAdditions atIndexes:indexes];
NSLog(@"array: %@", array);
 
// Output: array: (one, two, three, four, a, b)

If you replace [indexes addIndex:4] with [indexes addIndex:6] (so that the indexes are 5 and 6), then the application will fail with an out of bounds exception. あなたが[indexes addIndex:4][indexes addIndex:6]で置き換えるならば(その結果indexesは5と6です)、そのときこの適用は境界外例外で失敗するでしょう。

In this example, two objects are added into the middle of the array, and another at the current end of the array (index 4) which means that it is third from the end of the modified array. この例において、2つのオブジェクトは配列の真ん中に、そしてもう1つは配列の現在の終わり(インデックス4)で加えられます、それは修正後の配列の終わりから3番目です。


NSMutableArray *array = [NSMutableArray arrayWithObjects: @"one", @"two", @"three", @"four", nil];
NSArray *newAdditions = [NSArray arrayWithObjects: @"a", @"b", @"c", nil];
NSMutableIndexSet *indexes = [NSMutableIndexSet indexSetWithIndex:1];
[indexes addIndex:2];
[indexes addIndex:4];
[array insertObjects:newAdditions atIndexes:indexes];
NSLog(@"array: %@", array);
 
// Output: array: (one, a, b, two, c, three, four)

If you replace [indexes addIndex:4] with [indexes addIndex:6] (so that the indexes are 1, 2, and 6), then the output is (one, a, b, two, three, four, c). あなたが[indexes addIndex:4][indexes addIndex:6]で置き換えるならば(その結果indexesは1、2、そして6です)、そのとき出力は(one, a, b, two, three, four, c)です。

If objects or indexes is nil, this method will raises an exception. objectsまたはindexesnilならば、このメソッドは例外を引き起こすでしょう。

See Also 参照

Adding Objects オブジェクトを追加する