5

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]; 
Nimit Pattanasri
  • 1,602
  • 1
  • 26
  • 37

2 Answers2

8

It looks like dateAdded and/or validUntilDate are actually strings and not dates. Maybe they are strings representing dates, but strings after all.

albertamg
  • 28,492
  • 6
  • 64
  • 71
  • You could try to avoid it with `NSDate* dateAdded=(NSDate*)[eventDictionary objectForKey:@"dateAdded"];` – Paul Jun 25 '11 at 18:08
  • 6
    If the object returned by `objectForKey:` is not a `NSDate`, a cast won't make it become one. – albertamg Jun 25 '11 at 18:10
  • Not? In case the original variable for the key "dateAdded" is a NSDate* it should be returning a NSDate*. Or am I wrong? – Paul Jun 25 '11 at 18:11
  • If the value for the key "dateAdded" is a `NSDate` it will return a `NSDate` and you don't need the cast. If it is not a `NSDate`, the cast won't help. – albertamg Jun 25 '11 at 18:14
  • That's true. So I guess you will be right with your assumption. – Paul Jun 25 '11 at 18:15
  • it comes in Float value! enclosed in inverted commas, So that is why typcast do nothing :( By the way any solution/workaround ????? – AsifHabib May 01 '14 at 11:04
4

You need to use an NSDateFormatter to convert your date strings to actual NSDates.

uvesten
  • 3,365
  • 2
  • 27
  • 40