5

2 つの日付の違いを比較したいのですが、以下のコードを使用すると、「[NSCFString timeIntervalSinceReferenceDate]: 認識されないセレクターがインスタンスに送信されました」というエラーが発生しました。コードの何が問題になっていますか?

NSDate *dateAdded=[eventDictionary objectForKey:@"dateAdded"];
NSDate *validUntilDate=[eventDictionary objectForKey:@"validUntilDate"];
NSDateComponents *sometimeAgo = [[NSCalendar currentCalendar] components:(NSSecondCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:dateAdded  toDate:validUntilDate  options:0]; 
4

2 に答える 2

8

日付ではなく、実際には文字列のようdateAddedに見えます。validUntilDate日付を表す文字列かもしれませんが、結局のところ文字列です。

于 2011-06-25T18:06:25.323 に答える
4

日付文字列を実際の NSDates に変換するには、 NSDateFormatterを使用する必要があります。

于 2011-06-25T22:06:54.983 に答える