1

I have an NSDictionary that holds a date like this:

"2015-05-23 21:04:11 +0000"

enter image description here

and I'm trying to parse it like:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

NSDate *date = [dateFormat dateFromString:[item valueForKey:@"enddata"]];

I also tried changing the last line to:

NSDate *date = [dateFormat dateFromString:[[item valueForKey:@"enddata"] stringValue]];

but my error is always the same:

[__NSDate length]: unrecognized selector sent to instance 0x78ec0170

I've seen posts like this, or this, or this, and many more. I've tried with different formats such as:

  1. @"yyyy-MM-dd HH:mm:ss z"
  2. @"yyyy-MM-dd HH:mm:ss zzzz"
  3. @"yyyy-MM-dd HH:mm:ss Z"
  4. @"yyyy-MM-dd HH:mm:ss +zzzz"
  5. @"yyyy-MM-dd HH:mm:ss z"
  6. @"yyyy-MM-dd'T'HH:mm:ss'Z'"
  7. @"yyyy-MM-dd hh:mm:ss a"
  8. @"yyyy-MM-dd HH:mm:ss a"
  9. @"yyyy-MM-dd HH:mm:ss zzz"
  10. @"yyyy-MM-dd hh:mm:ss zzzz"

but still getting the same SIGABRT

Does anyone have a clue what's wrong?

Community
  • 1
  • 1
Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92

2 Answers2

2

I would hazard a guess that item[@"enddata"] is already an NSDate object and not an NSString object.

Verify with:

NSLog(@"enddata is type %@", NSStringFromClass([item[@"enddata"] class]));

Also don't use valueForKey:, but instead use objectForKey: or the newish access syntax I've used above.

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • 1
    oh, me = dumb... I'm so familiarized with Android than I didn't really wonder I could extract so "difficult" object from a dictionary.... Thank you very much !! – Rafael Ruiz Muñoz May 25 '15 at 17:16
0

This

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

NSDate *date = [dateFormat dateFromString:@"2015-05-23 21:04:11 +0000"];

NSLog(@"date: %@", [dateFormat stringFromDate:date]);

works fine for me.

NSLog is your friend. Maybe you should NSLog the value of item, or the value of

[item valueForKey:@"enddata"]

or even

[[item valueForKey:@"enddata"] stringValue]

since if the latter is an NSString in the desired format then calling dateFromString with it should work fine.

palimpsestor
  • 1,038
  • 1
  • 8
  • 28