簡體   English   中英

來自NSArray的最新消息

[英]Latest From NSArray

我在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];

我的返回值應該是明天(最新日期)。

您可以使用:

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

要么

[arr objectAtIndex:[arr count]-1];

您可以使用sortedArrayUsingComparator並分別從NSArray和NSDate類進行比較

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. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM