簡體   English   中英

如何在 NSDictionaries 的 NSArray 中為 distinctUnionOfObjects 使用 KVC 集合運算符?

[英]How can I use KVC Collection Operators for distinctUnionOfObjects in NSArray of NSDictionaries?

我想從具有值數組的數組字典中獲取特定屬性的集合。 以下是我的數組NSDictionaries

NSArray *arrGroupsFromServer= @[
    @{@"Id": @"7d1021ba-e9be-40c1-a0a1-ad2a2d185b74",@"Name": @"1",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"2"]},
    @{@"Id": @"4d830b79-f447-4ad7-8de1-063acf3e242c",@"Name": @"2 TEST",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"3"]},
    @{@"Id": @"e481987d-fde8-4a6b-8e05-73877065236d",@"Name": @"CQ",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"4"]},
    @{@"Id": @"1a44989d-8b3f-4d8e-8c74-a09c893ca02d",@"Name": @"Intervention group Number",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"5"]},
    @{@"Id": @"f10f2fe0-b575-450d-ad63-10d0e8fbe097",@"Name": @"EYFS Caterpillars",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"6"]},
    @{@"Id": @"232168c1-73a4-4bbb-b9a2-58e6765e4b2c",@"Name": @"Guided Reading group",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"6"]},
    @{@"Id": @"1e9b66f7-c4a7-4774-b062-ce23f587a319",@"Name": @"HfL",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"7"]},
    @{@"Id": @"62d53fc5-1fbf-4ebe-bc52-d07ec2680c66",@"Name": @"Red Intervention group",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"8"]},
    @{@"Id": @"a70e7429-996a-46ff-aff7-1e1bc5e17c3b",@"Name": @"SEN Green group",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"9"]},
    @{@"Id": @"44441c6f-843b-4933-9191-1f3afb23b6cf",@"Name": @"STAT",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"2"]},
    @{@"Id": @"04aedc28-4706-49a1-8bbd-9d58179cfd55",@"Name": @"TIST",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"2"]},
    @{@"Id": @"c98f592e-4adc-4d9d-a484-ae5b417f11db",@"Name": @"Year 8",@"modifiedDate": @"2016-01-29T15:39:57.000Z",@"PupilIdCollection":@[@"1",@"2"]}
];

現在我想將PupilIdCollection屬性收集到一個包含所有值的DistintctUnion的數組中,如下所示:

NSArray * PupilIdCollection=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];(e.g. Order doesn't matter)

我已經嘗試過以下方式:

NSArray *arrPupilsIdsFromServer = [arrGroupsFromServer valueForKeyPath:@"@distinctUnionOfObjects.PupilIdCollection"];

但是,這只是給了我不同的對象PupilIdCollection在數組arrPupilsIdsFromServer ,而不是在一個陣列中的所有值的數組如下的一個對象:

截屏

我希望在 KVC 中的一行代碼中包含以下內容:

NSArray * PupilIdCollection=PupilIdCollection=@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9"];(e.g. Order doesn't matter);(e.g. Order doesn't matter)

這該怎么做?

您需要使用@distinctUnionOfArrays而不是@distinctUnionOfObjects 此運算符有助於展平集合的層次結構並合並子數組。 有關詳細信息,請參閱開發人員文檔。

例如,這些行(用在你的 NSArray 上)

NSArray *arrPupilsIdsFromServer = [arrGroupsFromServer valueForKeyPath:@"@distinctUnionOfArrays.PupilIdCollection"];
NSLog(@"\nPupil Ids From Server:%@", arrPupilsIdsFromServer);

產生這個輸出:

Pupil Ids From Server:(
1,
2,
3,
4,
5,
6,
7,
8,
9
)

您可能想要它,但這並不意味着KVC可以做到-KVC尊重數據類型,並且不會為您弄平結構。 您需要編寫自己的flatten方法,以便可以傳遞數組數組並返回單個數組。

使用KVC集合運算符可能對您沒有幫助,因為您需要迭代所有數組內容並將其添加到集合中以獲得所需的最終結果,因此您也可以使用普通的KVC並避免數組重復處理(除非您知道會有大量重復的陣列)。

例如,從 wain 的答案中,您可以在獲取PupilIdCollection的值后執行此操作,

 NSArray *pupilIdCollection = [arrGroupsFromServer valueForKeyPath:@"PupilIdCollection"];
__block  NSMutableSet *mutableSet = [[NSMutableSet alloc] init];
 [pupilIdCollection enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    [mutableSet addObjectsFromArray:obj];
 }];
 pupilIdCollection = [mutableSet allObjects];
 NSLog(@"all objects-%@",pupilIdCollection);

韋恩解釋得很好。 我也覺得單行是很難實現的。 您可以為所有這些元素創建一個數組,然后使用distinctUnionOfObjects

NSMutableArray *singleArray =[NSMutableArray array];
for (NSDictionary *d in arrGroupsFromServer) {
    [singleArray addObjectsFromArray:[d objectForKey:@"PupilIdCollection"]];
}
NSArray *pupilIdCollection = [singleArray valueForKeyPath:@"@distinctUnionOfObjects.self"];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM