0

我在 NSArray 中有 NSdate 对象。我想从日期数组中获取最新信息。

NSDate *tomorrow = [NSDate dateWithTimeInterval:(24*60*60) sinceDate:[NSDate date]];

    NSDate *today = [NSDate date];

    NSDate *yesterday = [NSDate dateWithTimeInterval:-(24*60*60) sinceDate:[NSDate date]];


    NSMutableArray* arr = [[NSMutableArray alloc] initWithObjects:today,yesterday,tomorrow,today,yesterday,tomorrow, nil];

我的返回值应该是明天(最晚日期)。

4

2 回答 2

0

您可以使用:

[arr lastObject]; //Returns the object in the array with the highest index value.

或者

[arr objectAtIndex:[arr count]-1];
于 2012-08-14T06:14:47.310 回答
0

您可以使用sortedArrayUsingComparator并分别比较From NSArray & NSDate Class

NSArray *sortedDates = [arr sortedArrayUsingComparator: 
                                                     ^(id obj1, id obj2) {
                                                          return [obj1 compare:obj2];
                                                     }];
NSLog(@"%@",[sortedDates lastObject]); // SortedDates Contains dates in ascending Order (last object of the array returns the latest date. 
于 2012-08-14T06:22:49.263 回答