submittedTestArray = [{
scheduledTestId = "F7F15169-2FB6-4E47-A971-BAAD40D152AB";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "37F20871-FB3A-47AA-B254-9E5409C5E4C4";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "e18ead0a-d0fc-4e15-96ee-ecbd32880d97";
submittedTestId = "A46965C6-14B1-401E-97C5-AD5BADCC02EE";
}]
我希望所有 NSDictionary 对象唯一地基于scheduleTestId
如果我考虑上面的例子,那么它会给我两个 NSDictionary 对象。
我暂时使用以下代码。
NSMutableArray *uniqueKeys = [NSMutableArray arrayWithArray:[submittedTestArray valueForKeyPath:@"@distinctUnionOfObjects.scheduledTestId"]];
NSMutableArray *submittedTestUniqueArray = [[NSMutableArray alloc] initWithCapacity:uniqueKeys.count];
for (SubmittedTest *submittedTest in submittedTestArray) {
if ([uniqueKeys containsObject:submittedTest.scheduledTestId ]) {
[uniqueKeys removeObject:submittedTest.scheduledTestId];
[submittedTestUniqueArray addObject:submittedTest];
}
}
submitTestUniqueArray包含我想要的输出,即
[{
scheduledTestId = "F7F15169-2FB6-4E47-A971-BAAD40D152AB";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "37F20871-FB3A-47AA-B254-9E5409C5E4C4";
}, {
scheduledTestId = "440947DD-0A01-4DB8-8DD5-CCFA8F852FD3";
studentId = "8b3df16f-ff2f-4ad4-839c-6a937b79854d";
submittedTestId = "681689DC-B35C-491C-A737-AB19D9116FD9";
}]