我有从移动数组中删除重复对象的问题. 我已经尝试了这些: noDuplicates = _personalHistory.personalHistory;for (int i=[noDuplicates count]-1; i0; i--) { if ([noDuplicates indexOfObject: [noDuplicates objectAtIndex: i]]
我已经尝试了这些:
noDuplicates = _personalHistory.personalHistory; for (int i=[noDuplicates count]-1; i>0; i--) { if ([noDuplicates indexOfObject: [noDuplicates objectAtIndex: i]]<i) [noDuplicates removeObjectAtIndex: i]; } for (PersonalHistory_artikels *e in _personalHistory.personalHistory) { if (![noDuplicates containsObject:e]) { NSLog(@"Dubplicates"); [noDuplicates addObject:e]; } } for (i=0; i<_personalHistory.personalHistory.count; i++) { PersonalHistory_artikels *test = [_personalHistory.personalHistory objectAtIndex:i]; for (j=0; j<_personalHistory.personalHistory.count; j++) { PersonalHistory_artikels *test2 = [_personalHistory.personalHistory objectAtIndex:j]; if (! [test.nieuwsTITLE_personal isEqual:test2.nieuwsTITLE_personal]) { NSLog(@"Add test = %@", test.nieuwsTITLE_personal); [noDuplicates addObject:test]; } } }
但上面没有一个给我正确的阵列.最后一个是最好的,但它仍显示重复值.有人可以帮我解决这个问题吗?
非常感谢你.
编辑:
请注意,集合没有排序顺序.因此,您可以更便宜并放弃订单,或者进行稍微昂贵的操作但保留订单.
NSArray *hasDuplicates = /* (...) */; NSArray *noDuplicates = [[NSSet setWithArray: hasDuplicates] allObjects];