Help converting this string in "Xcode" please

Hello,

I have this…

NSLog(@“End Date, %@”, [dateResult1 stringValue]);
and it gives End Date, 25/8/13

How can I convert it like this
NSDate *endDate =

Thanks.

Lennox

Are you asking how to convert an NSString to an NSDate? If so, check out NSDateFormatter’s dateFromString: method.

OK Gavin, thanks,

I was trying it but could not get it right, I finally got this…

[self updateDateResult]; //This will ensure that a date is in the dateResult1 label

NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"dd-MMMM-yyyy"];
NSString *formattedDateString;


[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
formattedDateString = [formatter stringFromDate:[dateResult1 stringValue]];
NSDate *endDate = [formatter dateFromString:[dateResult1 stringValue]]; // Text in dateResult1 label is Monday, August 26, 2013

That works, the app compiles and no errors in log, but I am getting this"Semantic Issue" in the compiler (Xcode)…

Incompatible pointer types sending ‘NSString *’ to parameter of type ‘NSDate *’

How can I fix that?

Thanks.

Lennox

…the text says it all: you are assigning an NSString to an NSDate somewhere (on the line with the yellow mark “Semantic Issue” in XCode).

From the Apple website for NSDateFormatter:[quote]- (NSDate *)dateFromString:(NSString *)string
A date representation of string interpreted using the receiver’s current settings.[/quote]
The receiver’s current settings is “dd-MMMM-yyyy”, and you are trying to parse a string “Monday, August 26, 2013”, which will result in endDate being nil.

And what are these lines for - if you only want to parse a date, they are not needed:

NSString *formattedDateString; [formatter setDateStyle:NSDateFormatterFullStyle]; [formatter setTimeStyle:NSDateFormatterNoStyle]; formattedDateString = [formatter stringFromDate:[dateResult1 stringValue]];

Thanks Lukas,

If I comment those lines the app compiles without and errors but results in a null date.

Lennox

OK Lukas,

I got it…

I changed this

formattedDateString = [formatter stringFromDate:[dateResult1 stringValue]];
to
formattedDateString = [dateResult1 stringValue];

and all seems to be fine now.

Thanks for the insight.

Lennox