2012-04-24 5 views
2

From the iOS docs:부정적인 NSTimeInterval :

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

는 가까운 과거의 반 임의의 날짜와 시간을 생성합니다.

+6

부호 없음 INT의 부정적인는 양수이다. –

+0

@HotLicks 부정적인 문제가 발생하기 전에 NSTimeInterval에 전송 해 주셔서 감사합니다. – Tyilo

답변

7

핫 릭 (Hot Licks)이 말했듯이, 번호를 부정하고 이후에 캐스팅하지 않고 숫자를 음수로 만든 후에 NSTimeInterval로 캐스팅했기 때문입니다.

라인의 고정 된 버전은 다음과 같이 표시됩니다

NSDate* date = [NSDate dateWithTimeIntervalSinceNow:-(NSTimeInterval)((u_int32_t)i * 12 * 60 * 60 + arc4random_uniform(8 * 60 * 60))]; 
+0

감사합니다. 완벽하게 작동했습니다. – jungledev