NSMutableArray 和 NSArray 初始化

发布于 2025-01-03 12:31:39 字数 1766 浏览 2 评论 0原文

在 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 */
}

Is there a better way to initialize a 2 dimensional array in objective-c than this:

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 */
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

顾铮苏瑾 2025-01-10 12:31:39

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

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

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

I cann't find regularity in your arrays. At least you can wrap some code into cycle

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

for (int i = 0; i<9; ++i){
    [template addObject: obj];
}
逆流 2025-01-10 12:31:39

假设你想创建一个 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]];
}

assume you want to make a 2d array of 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]];
}
梦忆晨望 2025-01-10 12:31:39

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

Use nsdictionary to hold numbers and then store the dictionary in nsarray

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文