如何计算Objective-C中多维数组中的元素数

时间:2016-08-30 03:20:49

标签: ios objective-c nsmutablearray

我创建了一个像这样的多维数组:

Array = [NSMutableArray arrayWithObjects:
                 [NSMutableArray array],
                 [NSMutableArray array],nil];

[Array addObject:@"0"];
[Array addObject:@"1"];
[Array addObject:@"2"];

[[Array objectAtIndex:0] addObject:@"5"];
[[Array objectAtIndex:0] addObject:@"6"];
[[Array objectAtIndex:1] addObject:@"7"];
[[Array objectAtIndex:1] addObject:@"8"];
[[Array objectAtIndex:1] addObject:@"9"];

我想计算数组中“7”,“8”,“9”(索引1处的对象)中元素的数量。 我编写了代码,但它没有用。

NSInteger count = [Array[1] count];

如何计算仅数组(索引)的元素数?

1 个答案:

答案 0 :(得分:0)

请检查以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSMutableArray* myArray = [[NSMutableArray alloc] initWithObjects:[[NSMutableArray alloc] init],[[NSMutableArray alloc] init], nil];

    [[myArray objectAtIndex:0] addObject:@"test00"];
    [[myArray objectAtIndex:0] addObject:@"test01"];
    [[myArray objectAtIndex:0] addObject:@"test02"];

    [[myArray objectAtIndex:1] addObject:@"test10"];
    [[myArray objectAtIndex:1] addObject:@"test11"];

    NSUInteger array0Count = [[myArray objectAtIndex:0] count];
    NSUInteger array1Count = [[myArray objectAtIndex:1] count];



    NSLog([NSString stringWithFormat:@"array0.count=%lu,array1.count=%lu",(unsigned long)array0Count,(unsigned long)array1Count]);
}

希望它可以帮到你。