如何比较 NSDates 以查找数组中最早和最新的日期?

发布于 2024-12-27 02:22:32 字数 580 浏览 1 评论 0原文

我的代码在确定数组中最早日期和最晚日期时遇到很多问题。这是我正在使用的代码:

NSDate *startDate = nil; // Earliest date
NSDate *endDate = nil; // Latest date

for (id entry in myArray) 
{
    NSDate *date = [entry objectForKey:kDate];

    if (startDate == nil && endDate == nil) 
    {
        startDate = date;
        endDate = date;
    }
    else if ([date compare:endDate] == NSOrderedAscending)
    {
        startDate = date;
    }
    else if ([date compare:startDate] == NSOrderedDescending)
    {
        endDate = date;
    }

    date = nil;
}

请有人帮我找出哪里出错了?

I'm having a lot of problems with my code that determines the earliest date and latest date in an array. Here is the code I'm using:

NSDate *startDate = nil; // Earliest date
NSDate *endDate = nil; // Latest date

for (id entry in myArray) 
{
    NSDate *date = [entry objectForKey:kDate];

    if (startDate == nil && endDate == nil) 
    {
        startDate = date;
        endDate = date;
    }
    else if ([date compare:endDate] == NSOrderedAscending)
    {
        startDate = date;
    }
    else if ([date compare:startDate] == NSOrderedDescending)
    {
        endDate = date;
    }

    date = nil;
}

Please can someone help me work out where I'm going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

感情废物 2025-01-03 02:22:32

您还可以使用 NSDates 与方法对 NSArray 进行排序:

array = [array sortedArrayUsingSelector:@selector(compare:)];

然后在 [array firstObject] 将是第一个日期(例如 1970-01-01T00:00 :00),最后一个对象:[array lastObject] 将是最后一个日期:(例如 2011-01-12T00:00:00)

Also you can sort NSArray with NSDates with method:

array = [array sortedArrayUsingSelector:@selector(compare:)];

Then at [array firstObject] will be first date (e.g. 1970-01-01T00:00:00), and last object: [array lastObject] will be last date: (e.g. 2011-01-12T00:00:00)

素衣风尘叹 2025-01-03 02:22:32

您肯定在 else if 语句中以错误的方式设置了 startDateendDate 吗?你不想要这个吗:

NSDate *startDate = nil; // Earliest date
NSDate *endDate = nil; // Latest date

for (id entry in myArray) 
{
    NSDate *date = [entry objectForKey:kDate];

    if (startDate == nil && endDate == nil) 
    {
        startDate = date;
        endDate = date;
    }
    if ([date compare:startDate] == NSOrderedAscending)
    {
        startDate = date;
    }
    if ([date compare:endDate] == NSOrderedDescending)
    {
        endDate = date;
    }

    date = nil;
}

Surely you're setting startDate and endDate the wrong way round in the else if statements? Don't you want this:

NSDate *startDate = nil; // Earliest date
NSDate *endDate = nil; // Latest date

for (id entry in myArray) 
{
    NSDate *date = [entry objectForKey:kDate];

    if (startDate == nil && endDate == nil) 
    {
        startDate = date;
        endDate = date;
    }
    if ([date compare:startDate] == NSOrderedAscending)
    {
        startDate = date;
    }
    if ([date compare:endDate] == NSOrderedDescending)
    {
        endDate = date;
    }

    date = nil;
}
烛影斜 2025-01-03 02:22:32

您还可以使用预定义的 遥远的过去distantFuture 常量以避免对 nil 进行额外检查:

NSDate *startDate = [NSDate distantFuture];
NSDate *endDate = [NSDate distantPast];

for (id entry in myArray)
{
    NSDate *date = [entry objectForKey:kDate];

    if ([date compare:startDate] == NSOrderedAscending) { startDate = date; }
    if ([date compare:endDate] == NSOrderedDescending)  { endDate = date; }

    date = nil;
}

You may also use the pre-defined distantPast and distantFuture constants to avoid the extra check for nil:

NSDate *startDate = [NSDate distantFuture];
NSDate *endDate = [NSDate distantPast];

for (id entry in myArray)
{
    NSDate *date = [entry objectForKey:kDate];

    if ([date compare:startDate] == NSOrderedAscending) { startDate = date; }
    if ([date compare:endDate] == NSOrderedDescending)  { endDate = date; }

    date = nil;
}
愁以何悠 2025-01-03 02:22:32

您还可以使用 earlierDate:laterDate: NSDate 函数

NSDate *startDate = [NSDate distantFuture];
NSDate *endDate = [NSDate distantPast];

for (id entry in myArray) 
{
    NSDate *date = [entry objectForKey:kDate];
    startDate = [startDate earlierDate:date];
    endDate = [endDate laterDate:date];
}

You could also use the earlierDate: and laterDate: NSDate functions

NSDate *startDate = [NSDate distantFuture];
NSDate *endDate = [NSDate distantPast];

for (id entry in myArray) 
{
    NSDate *date = [entry objectForKey:kDate];
    startDate = [startDate earlierDate:date];
    endDate = [endDate laterDate:date];
}
雨轻弹 2025-01-03 02:22:32

对于其他类似的问题:我认为 valueForKeyPath: 选项非常简洁:

[myArray valueForKeyPath:@"@min.self"];
[myArray valueForKeyPath:@"@max.self"];

这假设数组中的对象按照您想要的方式实现 compare: 方法。例如,我用它来获取日期数组中的最早日期。如果您想在此处使用它,则数组中的对象应该覆盖 compare: 方法。

Just for other similar problems: the valueForKeyPath: option is very neat I think:

[myArray valueForKeyPath:@"@min.self"];
[myArray valueForKeyPath:@"@max.self"];

This assumes the object in the array implement the compare: method the way you want it. I use it to get the earliest date in an array of dates for example. If you would like to use it here, the objects in the array should override the compare: method though.

悲念泪 2025-01-03 02:22:32

最早的日期可以通过使用标准算法来细化数组中的最小/最大值来找到:

NSMutableArray<NSDate *> *allDates; // dates array
NSDate *earliestDate = allDates.firstObject;
        for (NSDate *date in allDates) {
            if ([date compare:earliestDate] == NSOrderedAscending) {
                earliestDate = date;
            }
        }

要查找数组中的最新日期,请使用 NSOrderedDescending 而不是 NSOrderedAscending。

The earliest date might be found by using the standard algorithm to fine min/max value in array:

NSMutableArray<NSDate *> *allDates; // dates array
NSDate *earliestDate = allDates.firstObject;
        for (NSDate *date in allDates) {
            if ([date compare:earliestDate] == NSOrderedAscending) {
                earliestDate = date;
            }
        }

To find the latest date in array use NSOrderedDescending instead of NSOrderedAscending.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文