conversión de cadena a NSDate problema: [__NSDate Length] no reconocido seleccionado

indexNumber.row is a date like 2012-03-09 23:00:00 +0000. I've tried a handful of different ways to get my date into an NSDate, but I keep getting the title error.

NSString *inputString = [self.measurements objectAtIndex:indexNumber.row];

// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:inputString ];
NSLog(@"date format is: %@", date);

Any ideas why I get the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x6ca0c60'

preguntado el 09 de marzo de 12 a las 16:03

What error do you get, could you be a bit more specific? -

2 Respuestas

You get that error because you are passing a NSDate oponerse a dateFromString:.

Añadir un NSLog and you will see that:

NSString *inputString = [self.measurements objectAtIndex:indexNumber.row];
if ([inputString isKindOfClass:[NSDate class]]) {
    NSLog(@"inputString is of type NSDate");

Also, here is how to reproduce your error:

NSString *inputString = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = [dateFormatter dateFromString:inputString];

That means that the array self.measurements contiene NSDate objetos y no NSString objetos.

When you correct your current problem, you will probably encounter a new one because you don't use the correct date format. To parse date strings of type: 2012-03-09 23:00:00 +0000 you should use the following date format:

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zz"];

respondido 09 mar '12, 17:03

i've tried a few different setDateformats. I get the same error with the above format: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDate length]: unrecognized selector sent to instance 0x6ca0c60'" - BamBamBeano

added an NSLog after setting the string and get "date format is:2012-03-09 09:36:53 +0000" - BamBamBeano

nailed it. after running your if, it was in fact an NSDate and no conversion was necessary. thanks sch. - BamBamBeano

Either do as sch suggests, or if you don't need the time at all, you can just truncate the string you feed to your dateFormatter. E.g. like so:

NSDate *date = [dateFormatter dateFromString:[inputString substringToIndex:9]];

Dónde 9 is an index, up to which you want to take a chunk of string.

respondido 09 mar '12, 16:03

That won't solve his problem. If the problem was the date formatting, he would get a nil date and not the error he mentioned. - sch

If the problem is in the difference between date format and the format of given string - it solve the problem. If the string is cero, then of course it won't give anything, as well as you suggestion. - pastas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.