1
NSString *value = [newProperties objectForKey:key]
NSLog(@"%@",value);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss zzzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *dateFromString = [dateFormatter dateFromString:value];

value is 2015-02-13 17:16:00 +0000
But I am getting crash in last line which says :

'-[__NSTaggedDate length]: unrecognized selector sent to instance 0xe41ba8e68d000000'
Nitish
  • 13,845
  • 28
  • 135
  • 263
  • `value` is already an `NSDate`, not an `NSString`. There's nothing to convert. – rmaddy Jan 14 '15 at 18:26
  • BTW - If you actually did need to convert a string with that format, don't set the timezone on the date formatter and change the `zzzz` to `Z`. – rmaddy Jan 14 '15 at 18:27
  • @maddy: value is a string which has a date in it. – Nitish Jan 14 '15 at 18:28
  • @maddy: I tried with Z too. Not working. – Nitish Jan 14 '15 at 18:28
  • 3
    No. `value` is not a string. It is an `NSDate`. If it really was an `NSString` you wouldn't be getting that error. – rmaddy Jan 14 '15 at 18:28
  • You are calling the **length** which is not able to call. Because the sender is **NSDate** – Lucas Huang Jan 14 '15 at 18:30
  • @maddy: How may I make you believe value is a string. – Nitish Jan 14 '15 at 18:30
  • @nitish NSLog(@"Value class: %@", [value class]); – mbogh Jan 14 '15 at 18:31
  • I have written two more lines on top from my code. Please check. – Nitish Jan 14 '15 at 18:32
  • `[newProperties objectForKey:key];` can return anything. Please provide the output of `[value class]`. – mbogh Jan 14 '15 at 18:33
  • More specifically, `value` is a `__NSTaggedDate`. – Ian MacDonald Jan 14 '15 at 18:35
  • @Nitish When you log an `NSDate`, it looks exactly like the output you are seeing. Do what "mbogh" stated and log `[value class]`. It won't be a string class. It will be a date class. – rmaddy Jan 14 '15 at 18:37
  • Everyone : My mistake. class is coming out to be NSDate. I apologize. – Nitish Jan 14 '15 at 18:38
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks Jan 14 '15 at 18:54
  • By the way, if indeed you *were* converting a string date, you've got the wrong date format. "hh" means a 12-hour clock, and it will choke on "17" for the hour. You should use "HH". – Hot Licks Jan 14 '15 at 18:56
  • @HotLicks : How is this a duplicate of what you have mentioned? – Nitish Jan 14 '15 at 18:57
  • It's about "unrecognized selector", and how to debug that. (Why do you think rmaddy knew right away that you already had an NSDate?) – Hot Licks Jan 14 '15 at 18:58

1 Answers1

4

The error is clearly showing that value is already an NSDate, not an NSString.

All of your code can be replaced with one line:

NSDate *value = [newProperties objectForKey:key];

You can verify that value is a date by logging its class:

NSLog(@"value class = %@", [value class]);

As a side note. If you really did have a date string in the format shown, your format specifier would need to be yyyy-MM-dd HH:mm:ss Z.

rmaddy
  • 314,917
  • 42
  • 532
  • 579