Understanding Variable Length Arrays in Objective-c

Asked 1 years ago, Updated 1 years ago, 100 views

#defineMV_GRID_LENGTH1
#define MV_GRID_WIDTH2

@interfaceViewController(){
    UIImageView* imageView [MV_GRID_LENGTH*MV_GRID_WIDTH];
}

- (void) viewDidLoad{
    int cnt = 0;

    for(inti=0;i<MV_GRID_LENGTH;i++){
        for(int j=0;i<MV_GRID_WIDTH;j++){
            imageView [cnt] = [UIImageView new];
            // Processing summary

            [self.view addSubview: imageView [cnt]];
            cnt++;
        }
    }
}

If the number of imageView array elements becomes variable in the process of generating multiple imageViews at the same time as above, I think I should have expressed it like "UIImageView* imageView[];" in c language, but how should I express it in objctive-c?I'm sorry for the rudimentary question, but I appreciate your cooperation.

xcode objective-c

2022-09-30 21:32

1 Answers

A standard class is available to add and remove objects called NSMutableArray.
We have not verified the code operation, but
It looks like the following

NSMutableArray<UIImageView*>*imageViewArray=[NSMutableArray<UIImageView*>array];

for (inti=0;i<MV_GRID_LENGTH;i++)
{
    for (int j=0;i<MV_GRID_WIDTH;j++)
    {
        [imageViewArray addObject: [UIImageView new]];
        // Processing summary

        [self.view addSubview: imageViewArray objectAtIndex:cnt];
        cnt++;
    }
}


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.