Shaik Tamim Shaik Tamim - 5 months ago 13
iOS Question

finding the maximum date from an array in objective c

i have a mutable array with objects as dates,now i want to get the maximum date from an array..i tried like this

NSMutableArray *arrDates = [[NSMutableArray alloc]init];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd"];
for (int i = 0; i<dateArray2.count; i++)
{
[arrDates addObject:[df dateFromString:dateArray2[i]]];
}
NSLog(@"%@",arrDates);
NSDate *maxDate=[arrDates valueForKeyPath:@"@max.date"];
NSLog(@"max date is %@",maxDate);
}


while executing app has got terminate..can any one help me...thanks in advance

Answer

You can use,

NSDate *maxDate = [dateArray valueForKeyPath:@"@max.self"];

This will give you the largest date from array.

read more https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/CollectionOperators.html