【发布时间】:2012-04-24 23:59:12
【问题描述】:
seconds
The number of seconds from the current date and time for the new date. Use a negative value to specify a date before the current date.
然而,这段代码给出的日期是 2148:
#import <Foundation/Foundation.h>
#import <stdlib.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
for(int i = 0; i < 30; i++) {
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)-((u_int32_t)i * 12 * 60 * 60 + arc4random_uniform(8 * 60 * 60))];
NSLog(@"%@", date);
}
}
return 0;
}
它应该从最近的过去生成半随机的日期和时间。
【问题讨论】:
-
无符号整数的负数是正数。
-
@HotLicks 感谢您在修复负面之前转换为 NSTimeInterval。
标签: objective-c ios nsdate