Shaik Tamim Shaik Tamim - 1 year ago 70
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]]];
NSDate *maxDate=[arrDates valueForKeyPath:@""];
NSLog(@"max date is %@",maxDate);

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

Answer Source

You can use,

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

This will give you the largest date from array.

read more

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download