Objective-C
NSIndexSet

NSIndexSetの使い方 Objective-C

NSIndexSetの使い方

サンプル

SampleIndexSet.m
- (void)sample {

    //変更可能な配列を生成する
    NSMutableArray *fruitBasket;
    fruitBasket = [NSMutableArray arrayWithObject:@"Apple"];
    [fruitBasket addObject:@"pear"];

    NSArray *insertedFruit;
    insertedFruit = [NSArray arrayWithObjects:
                       @"Banana", @"Orange", nil];

    long first = (long)fruitBasket.count - 2;

    //2つのオブジェクトを挿入する
    NSIndexSet *indexes;
    indexes = [NSIndexSet indexSetWithIndexesInRange:
               NSMakeRange(first, first + insertedFruit.count)];

    NSLog(@"first : %ld",indexes.firstIndex);
    NSLog(@"last : %ld",indexes.lastIndex);

    [fruitBasket insertObjects:insertedFruit
                 atIndexes:indexes];

    NSLog(@"買い物かご:%@", fruitBasket);

    //変更可能なセットを生成する
    NSMutableArray *plasticShoppingBags;

    plasticShoppingBags = [[NSMutableOrderedSet orderedSetWithObject:@"Apple"] mutableCopy];
    first = plasticShoppingBags.count - 1;

    indexes = [NSIndexSet indexSetWithIndexesInRange:
               NSMakeRange(first, first + insertedFruit.count)];

    //2つのオブジェクトを挿入する
    [plasticShoppingBags insertObjects:insertedFruit
                    atIndexes:indexes];
    NSLog(@"レジ袋:%@", plasticShoppingBags);

実行結果

NSIndexSet.log
first : 0
last : 1

買い物かご:(
    Banana,
    Orange,
    Apple,
    pear
)
レジ袋:{(
    Banana,
    Orange,
    Apple
)}

制作チーム:サンストライプ

(NEWドメイン) http://sunstripe-main.jp/