拆分的NSMutableArray成不同的阵列
问题描述:
在一个可变的阵列我有多个阵列,在每一个阵列I具有多个词典,在词典我有共同的值,基于该值I要组的阵列/下面解释是样本阵列
(
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Fourth;
},
),
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Fifth;
},
),
(
{
name = "attribute_Subject";
value = Maths;
},
{
name = "attribute_class";
value = Fourth;
},
),
(
{
name = "attribute_Subject";
value = Science;
},
{
name = "attribute_class";
value = Fourth;
},
),
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Sixth;
},
),
(
{
name = "attribute_Subject";
value = Maths;
},
{
name = "attribute_class";
value = Sixth;
},
),
)
如果看到阵列我们有英文三种阵列,我想这三个阵列在一个单独的数组和同为“科学”和数学
我们能使用谓词?
任何一个帮助都可以
答
我认为这段代码会有帮助。 NSLog(@“%@”,muteArray);
NSArray *valuesArray = [muteArray valueForKey:@"value"];
NSMutableArray *valuesArray1 = [[NSMutableArray alloc]init];
for(NSArray *arr in valuesArray) {
[valuesArray1 addObject:[arr objectAtIndex:0]];
}
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:valuesArray1];
NSArray *arrayWithoutDuplicates = [orderedSet array];
NSArray *filtered = [muteArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"value Contains[d] %@", [arrayWithoutDuplicates objectAtIndex:0]]];
使用循环得到arrayWithoutDuplicates后你可以得到不同的阵列。
答
我不能完全肯定我明白你问什么,但我认为这应该有所帮助:
的Objective-C:
//1
NSMutableArray* englishArray = [[NSMutableArray alloc]init];
NSMutableArray* scienceArray = [[NSMutableArray alloc]init];
NSMutableArray* mathArray = [[NSMutableArray alloc]init];
//2
for(int i = 0; i < mainArray.count; i++){
//3
if([mainArray[i][0][@"value"] isEqual: @"English"]){
//4
[englishArray addObject:mainArray[i]];
}
else if([mainArray[i][0][@"value"] isEqual: @"Science"]){
[scienceArray addObject:mainArray[i]];
}
else if([mainArray[i][0][@"value"] isEqual: @"Math"]){
[mathArray addObject:mainArray[i]];
}
}
以上代码的解释:
- 创建3
NSMutableArrays
来存储三个主题数组。 - 遍历mainArray中的所有数组(其中mainArray是您提供的完整数组)
- 要检查主题,首先访问索引为i的mainArray中的数组。
例如, mainArray [i]:
(
{
name = "attribute_Subject";
value = English;
},
{
name = "attribute_class";
value = Fourth;
},
),
3.(续)然后访问刚才访问的数组中的第1个元素。 (第一个元素是包含主题值的词典)
例如, mainArray [i] [0]:
{
name = "attribute_Subject";
value = English;
},
3.(续2)然后访问该字典的key @“value”的值。
例如。 mainArray [I] [0] [@ “值”]:
@"English"
4.如果该键是等于我们需要,添加到适当的主题阵列的方向图的之一。
你能展示预期的结果吗? – Larme