2011-04-29 7 views
40

유닉스 타임 스탬프에서 NSDate를 만드는 방법은 무엇입니까?유닉스 타임 스탬프에서 NSDate 만들기

channel.startDate = [NSDate dateWithTimeIntervalSince1970: 
(NSTimeInterval)[channelJson objectForKey:@"broadcastStartedTime"]]; 

는이 오류를 얻을 :

104: error: pointer value used where a floating point value was expected

channels.startDateNSDate*입니다. 키 "broadcastStartedTime"의 값은 파서 라이브러리에 의해 NSNumber 또는 NSDecimalNumber으로 변환 된 자바 스크립트 Number입니다.

답변

51

대신이 시도 :

NSNumber *startTime = channelJson[@"broadcastStartedTime"]; 
channel.startDate = [NSDate dateWithTimeIntervalSince1970:[startTime doubleValue]]; 

귀하의 값이 NSNumber의 포인터 타입에 갇혀됩니다. dateWithTimeIntervalSince1970 메서드는 프리미티브 NSTimeInterval (덮개 아래는 double)을 필요로합니다.

4

당신은의 NSNumber 랩을 해제해야합니다

channel.startDate = [NSDate dateWithTimeIntervalSince1970:[[channelJson objectForKey:@"broadcastStartedTime"] doubleValue]]; 
7

사용 -doubleValue :

// NSTimeInterval is just a typedef for double 
NSTimeInterval interval = [[channelJson objectForKey:@"broadcastStartedTime"] doubleValue]; 
channel.startDate = [NSDate dateWithTimeIntervalSince1970:interval]; 
+0

내 타입 캐스팅과 함께 잘못 무엇입니까? – JoJo

+0

Objective-C 객체 (예 : NSNumber)를 프리미티브 (예 : double)로 캐스팅 할 수 없습니다. 컴파일 타임 캐스팅은 기본에서 원시 또는 개체에서 개체로 작동합니다. 그래서 우리는'doubleValue' 메소드를 호출해야했습니다. 런타임시 double로 변환합니다. –

0

NSTimeIntervalNSDate 변환 스위프트에 (유닉스 timestmap는 것입니다) :

let timeInterval = NSDate().timeIntervalSince1970 // the the unix timestamp 
NSDate(timeIntervalSince1970: timeInterval) 
관련 문제