2

iOS ドキュメントから:

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;
}

近い過去から半ランダムな日付と時刻を生成する必要があります。

4

1 に答える 1

7

Hot Licksが言ったように、最初に数値を否定して後でキャストするのではなく、数値を負にした後にNSTimeIntervalにキャストしたことが原因でした。

行の修正バージョンは次のようになります。

NSDate* date = [NSDate dateWithTimeIntervalSinceNow:-(NSTimeInterval)((u_int32_t)i * 12 * 60 * 60 + arc4random_uniform(8 * 60 * 60))];
于 2012-05-30T18:57:15.063 に答える