如何将数据插入 NSMutableArray 内的 NSMutableDictionary 内的 NSMutableArray?
How to Insert data into NSMutableArray that is inside NSMutableDictionary inside NSMutableArray?
我有一个包含多个 NSMutableDictionary 对象的 NSMutableArray,每个字典中都有一个 NSString 和一个 NSMutableArray。
我的问题是我需要根据字符串匹配找到正确的字典,找到后将一个对象插入到发现字符串匹配的同一字典中的 NSMutableArray 中,我只能在第一次完成这项工作当数组为空时,因为在那之后我无法匹配正确的字符串:
这是我到目前为止尝试过的方法,我尝试使用包含对象,但这不会让我进入数组中的字典,所以我被卡住了
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[@"name"];
NSString *value = variant[@"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:@"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:@"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[@"name"];
NSString *value = variant[@"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[@"name"] isequalToString:name]) {
NSMutableArray *values = v[@"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:@"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:@"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}
我有一个包含多个 NSMutableDictionary 对象的 NSMutableArray,每个字典中都有一个 NSString 和一个 NSMutableArray。
我的问题是我需要根据字符串匹配找到正确的字典,找到后将一个对象插入到发现字符串匹配的同一字典中的 NSMutableArray 中,我只能在第一次完成这项工作当数组为空时,因为在那之后我无法匹配正确的字符串:
这是我到目前为止尝试过的方法,我尝试使用包含对象,但这不会让我进入数组中的字典,所以我被卡住了
NSMutableArray *variantOptions = [[NSMutableArray alloc] init];
for (NSDictionary *variant in variantInfo) {
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [[NSMutableArray alloc] init];
NSString *name = variant[@"name"];
NSString *value = variant[@"value"];
if(variantOptions.count > 0) {
// Need to loop through the array until name isEquaToString variantOptionName and when true insert value into variantOptionValuesArray in that same dict and if not exists create a new dict and insert in array
} else {
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:@"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:@"variantOptionValues"];
}
[variantOptions addObject:variantOption];
}
for (NSDictionary *variant in variantInfo) {
NSString *name = variant[@"name"];
NSString *value = variant[@"value"];
BOOL found = false;
for (NSMutableDictionary *v in variantOptions) {
if (v[@"name"] isequalToString:name]) {
NSMutableArray *values = v[@"variantOptionValues"];
[values addObject:value];
found = true;
break;
}
}
if (!found) {
// name was not found
NSMutableDictionary *variantOption = [[NSMutableDictionary alloc] init];
NSMutableArray *variantOptionValues = [NSMutableArray alloc] init];
[variantOptionValues addObject:value];
[variantOption setObject:name forKey:@"variantOptionName"];
[variantOption setObject:variantOptionValues forKey:@"variantOptionValues"];
[variantOptions addObject:variantOption];
}
}