检查 NSDate 对象是否在过去

Check if a NSDate object is in the past

我想检查我拥有的 NSDate 对象是过去还是现在(如果不是未来)。

我试过了,但没用:

-(void)checkIfMuted
{
    NSDate *muteOverDate=[[NSUserDefaults standardUserDefaults] objectForKey:@"muteOverDate"];

    if ([muteOverDate timeIntervalSinceNow]<0.0) {
        self.muteEnabled=YES;
        return;
    }
    self.muteEnabled=NO;
    NSLog(@"%@\n%@",[[NSDate date] description],[muteOverDate description]);
}

NSLog 打印: 2016-02-10 14:55:19 +0000 2016-02-10 15:54:32 +0000

知道为什么它不起作用吗?谢谢!

使用[NSDate compare:]:

NSDate *now = [NSDate date];
if ([now compare:muteOverDate] == NSOrderedAscending) {
    // muteOverDate is in the future
}