I'm trying to come up with the best / cleanest way to do this.
Basically I have an array of NSDates, and I'm trying to get the UITableView Delegate methods (numberOfSectionsInTableView) to return the number of dates in the array that aren't the same.
So here's another example:
My array:
NSDate *1 (Monday 1st Jan)
NSDate *2 (Monday 1st Jan)
NSDate *3 (Tuesday 2nd Jan)
NSDate *4 (Wednesday 3rd Jan)
NSDate *5 (Wednesday 3rd Jan)
The array I would like:
NSDate *1 (Monday 1st Jan)
NSDate *2 (Tuesday 2nd Jan)
NSDate *3 (Wednesday 3rd Jan)
Probably the easiest solution is to pass you dates array in a set and then again into an array, but you loose sorting.
By definition NSSet
can contain only one instance of equal instances.
NSSet *set = [NSSet setWithArray: datesArray];
By calling isEqual
to one of each elements is guaranteed that you can't have duplicated dates. isEqual
in a NSDate
calls isEqualToDate
that considers equality only if the instances represent the same date.
Then:
NSArray * newdates = set.allObjects
Is worth to mention that on NSArray you can also use KVC to make a distinct union:
NSArray * uniqueDates = [datesArray valueForKeyPath:@"@distinctUnionOfObjects.self"];