NSArray/NSMutableArray関連のメモ


```objectivec:NSArray
    NSArray *array = @[@"要素1", @"要素2", @"要素3"];
    NSLog(@"%@", array);
    array = @[@1, @2, @3, @YES, @NO];
    NSLog(@"%@", array);
    int a = 1;
    float b = 0.2;
    double c = 33.3;
    array = @[@(a), @(b), @(c)];
    NSLog(@"%@ [1]=[%@] count=[%lu] last=[%@]", array, array[1], array.count, array.lastObject);
    NSLog(@"%d %f %f", [array[0] intValue], [array[1] floatValue], [array[2] doubleValue]);
    
    // ループ.
    for (NSString *num in array) {
        NSLog(@"%d %f %f", [num intValue], [num floatValue], [num doubleValue]);
    }
	// 逆順.
    for (NSString *num in [array reverseObjectEnumerator]) {
        NSLog(@"%d %f %f", [num intValue], [num floatValue], [num doubleValue]);
    }
    // インデックス参照付き.
    [array enumerateObjectsUsingBlock:^(NSString *str, NSUInteger index, BOOL *stop) {
        NSLog(@"str: %@ (%lu)", str, index);
        if (index >= 1) {
            *stop = YES;
        }
    }];
    // 逆順.
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"obj: %@ (%lu)", obj, idx);
    }];
    
    // 追加、削除.
    NSMutableArray *marray = [NSMutableArray array];
    [marray addObject:@"要素1"];
    [marray addObjectsFromArray:@[@"要素2", @"要素3"]];
    NSLog(@"%@", marray);
    int i;
    NSInteger n;
    for (i=0; i < 10; i++) {
        n = arc4random() % 10;
        [marray addObject:@(n)];
    }
    NSLog(@"%@", marray);
    marray = [NSMutableArray arrayWithArray:@[@"a", @"b", @"c"]];
    [marray insertObject:@"d" atIndex:1];
    NSLog(@"%@", marray);
    [marray removeObjectAtIndex:1];
    NSLog(@"%@", marray);
    [marray removeObject:@"b"];
    NSLog(@"%@", marray);
    
    // 2次元.
    marray = [NSMutableArray array];
    [marray addObject:@[@"1-1", @"1-2", @"1-3"]];
    [marray addObject:@[@"2-1", @"2-2", @"2-3"]];
    [marray addObject:@[@"3-1", @"3-2", @"3-3"]];
    NSLog(@"%@", marray);
    NSLog(@"%@", marray[1]);
    NSLog(@"%@", marray[1][1]);
    
    // 検索.
    array = @[@"abc", @"def", @"ghi"];
    NSUInteger index = [array indexOfObject:@"def"];
    if (index != NSNotFound) {
        NSLog(@"index=%d", index);
    }
    else {
        NSLog(@"ありません");
    }
    BOOL isFound = [array containsObject:@"aaa"];
    if (isFound) {
        NSLog(@"あります");
    }
    else {
        NSLog(@"ありません");
    }
    
    // ソート.
    array = @[@"Bca", @"Acb", @"cab", @"Cba", @"bAc", @"aCB"];
    NSArray *sortArray = [array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    NSLog(@"%@", sortArray);
    sortArray = [array sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"" ascending:NO]]];
//    sortArray = [array sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"" ascending:NO selector:@selector(caseInsensitiveCompare:)]]];
    NSLog(@"%@", sortArray);
    marray = [NSMutableArray array];
    for (i=0; i < 30; i++) {
        n = arc4random() % 30;
        [marray addObject:@(n)];
    }
    NSLog(@"%@", marray);
    sortArray = [marray sortedArrayUsingFunction:intSort context:nil];
    NSLog(@"%@", sortArray);
    
    // 逆順.
    array = @[@"1", @"2", @"3"];
    NSArray *reverseArray = [[array reverseObjectEnumerator] allObjects];
    NSLog(@"%@", reverseArray);
    
    // フィルタリング.
    marray = [NSMutableArray arrayWithArray:@[@"One", @"Two", @"Three", @"Four", @"Five", @"Six", @"Seven", @"Eight", @"Nine", @"Ten"]];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K beginswith %@", @"self", @"T"];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains %@", @"self", @"e"];
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"length == 3"];
    [marray filterUsingPredicate:predicate];
    NSLog(@"%@", marray);
    marray = [NSMutableArray arrayWithArray:@[@3, @6, @9, @12, @15, @18, @11, @10, @8, @9]];
    predicate = [NSPredicate predicateWithFormat:@"%K >= %d", @"self", 10];
    [marray filterUsingPredicate:predicate];
    NSLog(@"%@", marray);
```