可变数组的二维NSMutableArray,但不知道我需要多少

时间:2022-04-02 23:07:41

I have a large array (a few hundred objects), and I need to separate it into several arrays depending on an integer in the object, but I don't know how many arrays I'll need. I thought using a two-dimensional NSMutableArray would work, but if I do it as seen below, then when I empty the tempArray, it empties the array in fullArray as well. Is there another way to use a temporary, reusable array that, once it's been added to another array, it releases references to it.

我有一个大型数组(几百个对象),我需要根据对象中的整数将它分成几个数组,但我不知道我需要多少个数组。我认为使用二维NSMutableArray会起作用,但如果我这样做,那么当我清空tempArray时,它也会清空fullArray中的数组。是否有另一种方法可以使用临时的,可重用的数组,一旦它被添加到另一个数组,它就会释放对它的引用。

- (void)createArray{

fullArray=[[NSMutableArray alloc]init];
NSMutableArray *tempArray=[[NSMutableArray alloc] init];

for(int j=0; j<numberOfGames; j++){

    for(int i=0; i<[appDelegate.hiddenChars count]; i++){
        Chars *charObj=[appDelegate.hiddenChars objectAtIndex:i];
        if(charObj.gameID==j){
            //NSLog(@"Match!");
            [tempArray addObject:charObj];
        }
    }
    [fullArray addObject:tempArray];
    [tempArray removeAllObjects];   //this empties it from fullArray too
}

}

I can get a variable for how many rows and columns, but they're not static. I've tried using C arrays, but I won't be able to make a global array that way. I tried defining a global array and using "new" in create array, but Xcode says "new" is unrecognized. I tried

我可以获得有多少行和列的变量,但它们不是静态的。我已经尝试过使用C数组,但是我无法以这种方式创建全局数组。我尝试定义一个全局数组并在create array中使用“new”,但Xcode表示“new”无法识别。我试过了

id fullArray;//in global scope
fullArray=new id fullArray[rows][columns];//new and id throw exceptions, new unrecognized and id expects ";" before it.

My other thought is to create a singleton, but it seems like overkill for this problem. Surely there's a way to handle needing an unknown number of arrays?

我的另一个想法是创建一个单身人士,但这个问题似乎有些过分。当然有办法处理需要未知数量的数组?

I'll be using the arrays to populate a grouped tableview with multiple sections. Maybe I'm going about this all wrong?

我将使用数组来填充具有多个部分的分组表视图。也许我说这一切都错了?

Thanks for any help.

谢谢你的帮助。

1 个解决方案

#1


1  

Change just about 2 lines and you've got it.

改变大约2行,你就知道了。

- (void)createArray{

    fullArray=[[NSMutableArray alloc]init];

    for(int j=0; j<numberOfGames; j++){
        // inside loop
        NSMutableArray *tempArray=[[NSMutableArray alloc] init];

        for(int i=0; i<[appDelegate.hiddenChars count]; i++){
            Chars *charObj=[appDelegate.hiddenChars objectAtIndex:i];
            if(charObj.gameID==j){
                //NSLog(@"Match!");
                [tempArray addObject:charObj];
            }
        }
        [fullArray addObject:tempArray];
        [tempArray release]; // but fullArray still owns it
    }

}

#1


1  

Change just about 2 lines and you've got it.

改变大约2行,你就知道了。

- (void)createArray{

    fullArray=[[NSMutableArray alloc]init];

    for(int j=0; j<numberOfGames; j++){
        // inside loop
        NSMutableArray *tempArray=[[NSMutableArray alloc] init];

        for(int i=0; i<[appDelegate.hiddenChars count]; i++){
            Chars *charObj=[appDelegate.hiddenChars objectAtIndex:i];
            if(charObj.gameID==j){
                //NSLog(@"Match!");
                [tempArray addObject:charObj];
            }
        }
        [fullArray addObject:tempArray];
        [tempArray release]; // but fullArray still owns it
    }

}