Create multiple numbered variables based on a int

前端 未结 2 2040
Happy的楠姐
Happy的楠姐 2020-11-22 11:24

How would I create a number of NSDictionary variables using an array\'s count?

This is basically what I came up with, but I\'m not sure how to make this

2条回答
  •  太阳男子
    2020-11-22 12:14

    Create a mutable array and loop until you reach the original array's count, creating a dictionary and adding it to the mutable array on each iteration.

    Your code should look like this.

    NSMutableArray *dictionaries = [[NSMutableArray alloc] init];
    for (int i = 0; i < doesntContainAnother.count; i++) {
        [dictionaries addObject:[NSMutableDictionary dictionaryWithObject:[array1 objectAtIndex:i] forKey:[array2 objectAtIndex:i]]];
    }
    

    The approach of creating variables with numbers at the end of their names is an antipattern and not even possible in Objective-C. It's equivalent to an array, but clunkier.

提交回复
热议问题