[NSCFString timeIntervalSinceReferenceDate]:发送到实例的无法识别的选择器
我想比较两个日期之间的差异,但使用下面的代码我收到错误“[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];
I would like to compare difference between two dates, but with the code below I got the error "[NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance." What's wrong with the code?
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来
dateAdded
和/或validUntilDate
实际上是字符串而不是日期。也许它们是代表日期的字符串,但毕竟是字符串。It looks like
dateAdded
and/orvalidUntilDate
are actually strings and not dates. Maybe they are strings representing dates, but strings after all.您需要使用 NSDateFormatter 将日期字符串转换为实际的 NSDates。
You need to use an NSDateFormatter to convert your date strings to actual NSDates.