【问题标题】:NSMutableArray and NSArray initializationNSMutableArray 和 NSArray 初始化
【发布时间】:2012-02-29 16:09:51
【问题描述】:

有没有比这更好的方法来初始化objective-c中的二维数组:

NSNumber *a = [[NSNumber alloc] initWithInt:0];
NSNumber *b = [[NSNumber alloc] initWithInt:1];
NSMutableArray *template = [[NSMutableArray alloc] initWithCapacity:16];
switch (myInt) {
    case 0:
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, a, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, a, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, a, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, a, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        [template addObject:[[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil]];
        break;
/* more */
}

【问题讨论】:

  • 如果稍后更改“a”或“b”的值,是否会在添加到模板的所有数组中也发生更改?
  • @iOS 开发者 NSNumber 是不可变对象,其值无法更改
  • @xlc0212 非常感谢!但是如果数组填充了可变对象,它会在添加到模板的所有数组中发生变化,对吧?

标签: objective-c nsmutablearray nsarray


【解决方案1】:

我在您的数组中找不到规律性。至少你可以将一些代码包装到循环中

id obj = [[NSArray alloc] initWithObjects:b, b, b, b, b, b, b, b, nil];

for (int i = 0; i<9; ++i){
    [template addObject: obj];
}

【讨论】:

    【解决方案2】:

    假设你想创建一个 NSNumber 的二维数组

    const int templateContent[][8] = { // put your data to this array
        1, 1, 1, 1, 1, 1, 1, 1,
        1, 0, 1, 1, 1, 1, 1, 1,
        // more...
    };
    const int dim = sizeof(templateContent)/sizeof(int[8]);
    NSNumber *numbers[2]; // assume your only need two numbers
    for (int i = 0; i < sizeof(numbers)/sizeof(id); i++) {
        numbers[i] = [NSNumber numberWithInt:i];
    }
    NSMutableArray *template = [[NSMutableArray alloc] initWithCapacity:16];
    for (int i = 0; i < dim; i++) {
        [template addObject:[NSArray arrayWithObjects:
                             numbers[templateContent[i][0]], 
                             numbers[templateContent[i][1]], 
                             numbers[templateContent[i][2]], 
                             numbers[templateContent[i][3]], 
                             numbers[templateContent[i][4]], 
                             numbers[templateContent[i][5]], 
                             numbers[templateContent[i][6]], 
                             numbers[templateContent[i][7]], nil]];
    }
    

    【讨论】:

      【解决方案3】:

      使用 nsdictionary 保存数字,然后将字典存储在 nsarray 中

      【讨论】:

        猜你喜欢
        • 2012-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多