#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *firstArray;
@property (nonatomic, strong) NSMutableArray *secondArray;
@property (nonatomic, strong) NSMutableArray *thirdArray;
@property (nonatomic, strong) NSMutableArray *fourthArray;
@property (nonatomic, strong) NSMutableArray *combinationArray;//组合数组
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.combinationArray = [[NSMutableArray alloc] init];
//创建数据
self.firstArray = [NSMutableArray arrayWithObjects:@"1-黑色",@"1-白色",@"1-黄色",@"1-红色", nil];
self.secondArray = [NSMutableArray arrayWithObjects:@"2-AA",@"2-BB",@"2-CC",@"2-DD",@"2-EE",@"2-FF", nil];
self.thirdArray = [NSMutableArray arrayWithObjects:@"3-XXL",@"3-XL",@"3-L", nil];
self.fourthArray = [NSMutableArray arrayWithObjects:@"4-衬衫",@"4-T恤", nil];
NSMutableArray * allArray = [[NSMutableArray alloc]initWithObjects:self.firstArray,self.secondArray,self.thirdArray,self.fourthArray, nil];
NSMutableArray * addArray = [[NSMutableArray alloc] init];
[self getCombination:0 allArray:allArray addArray:addArray];
}
#pragma mark - 获取多维数组组合
- (void)getCombination:(NSInteger)kid allArray:(NSMutableArray *)allArray addArray:(NSMutableArray *)addArray
{
if (kid < allArray.count) {
NSMutableArray * dataArr = [allArray objectAtIndex:kid];
if (dataArr.count == 0) {
return;
}
for (int i = 0; i < dataArr.count; i ++) {
NSMutableArray * newArr = [NSMutableArray new];
newArr = [addArray mutableCopy];
[newArr addObject:dataArr[i]];
[self getCombination:kid+1 allArray:allArray addArray:newArr];
}
}else
{
NSMutableArray * dataArr = [NSMutableArray new];
for (int i = 0; i < addArray.count; i ++ ) {
[dataArr addObject:addArray[i]];
}
[self.combinationArray addObject:dataArr];
NSLog(@"组合--->%@,组合数量-->%lu",self.combinationArray,(unsigned long)self.combinationArray.count);
}
}
@end
直接上代码自己运行一下就知道了。