Xcode 日期预处理器宏

Xcode preprocessor macro for dates

我想 "hardcode" 在我的测试代码中添加到期日期。现在我手动计算一个 unix 日期并将其与当前日期时间进行比较:

 if([[NSDate date] timeIntervalSince1970]>1422748800) mustHalt = TRUE;

我想要一种用宏替换 1422748800 的方法,该宏在编译时为未来 90 天的日期生成等效数字。

有什么建议吗?

预定义的宏__DATE__就是您所需要的。 Here is a SO question 与此相关。但也许您想使用这样的代码:

    const int daysToExpire = 14;

    NSString *compileDate = [NSString stringWithUTF8String:__DATE__];
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"MMM d yyyy"];
    NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [df setLocale:usLocale];
    NSDate *expireDate = [df dateFromString:compileDate];

    bool isExpired = ([[NSDate date] compare:expireDate] == NSOrderedDescending); // decide for it