gpt4 book ai didi

objective-c - 过去的 timeIntervalSinceDate?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:59 24 4
gpt4 key购买 nike

我在过去(过去 1 小时)创建了一个 NSDate,看起来设置正确,唯一的问题是我想用它来确定事件是否发生。因为我设置的是过去,所以当我检查时它肯定认为 even 已经发生了,但是 timeIntervalSinceDate 似乎只给出了一个肯定的结果?

这是我使用 timeIntervalSinceDate 的代码

 NSDate *now = [NSDate date];
NSTimeInterval secondsSincePlantingInterval = [now timeIntervalSinceDate:plantingDate];

哪个给出了 68 秒,但它应该是 -68 秒,或者它不返回负值?

最佳答案

这是完全正常的、有记录的行为

documentation状态:

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

Return Value

The interval between the receiver and anotherDate. If the receiver is earlier than anotherDate, the return value is negative.

因为 plantingDate 是过去的,接收者早于它。因此该值为正数。

此外;这是简单的英语

 [now timeIntervalSinceDate:plantingDate];

因此,从 plantingDate 到现在的时间间隔为正。

关于objective-c - 过去的 timeIntervalSinceDate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13351452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com