我正在尝试以多对一的有序 CoreData 关系替换对象...我的代码在这里:
NSMutableOrderedSet *battingLineUpTemp = [battingOrderToChange.battingOrder mutableCopy];
NSUInteger newBatsmanIndex = 0;
NSLog(@"Original batting order");
for (CricketPlayer *p in battingLineUpTemp) {
NSLog(@"switching : %@ %@",p.firstName, p.lastName);
}
for (int i = [innings.wicketsFallen integerValue] +2; i < [battingOrderToChange.battingOrder count]; i++) {
//[battingLineUpTemp replaceObjectAtIndex:i withObject:batsmenArray[newBatsmanIndex]];
[battingLineUpTemp replaceObjectAtIndex:i withObject:[batsmenArray objectAtIndex:newBatsmanIndex]];
newBatsmanIndex++;
}
NSLog(@"Changed batting order");
for (CricketPlayer *p in battingLineUpTemp) {
NSLog(@"switching : %@ %@",p.firstName, p.lastName);
}
[battingOrderToChange willChangeValueForKey:@"battingOrder"];
[battingOrderToChange setBattingOrder:[[NSOrderedSet alloc] initWithOrderedSet:[battingLineUpTemp copy]]];
battingOrderToChange.battingOrder = battingLineUpTemp;
[battingOrderToChange didChangeValueForKey:@"battingOrder"];
然而,第二次输出球员姓名时,我得到的结果与之前完全相同!我已确保我传递给该方法的 NSArray (batsmenArray) 与 *battingLineUpTemp 的顺序不同...为什么我的代码拒绝让我更改此 NSMutableOrderedSet 的顺序!?CoreData 是不是又一次让人头疼?
编辑 1 我还通过 for 循环中的 NSLogging 确保我要替换的对象和它要替换的对象不同...... NSMutableOrderedSet 只是拒绝重新排列!