Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSInvalidArgumentException, reason: 'Invalid type in JSON write (__NSDate)'

Tags:

I'm getting this exception when I try to JSON encode NSDate object.I believe NSDate is not compatible for JSON encoding. but I must encode the date.Any solutions?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)' 
like image 477
Harikrishnan Avatar asked Jan 15 '13 12:01

Harikrishnan


2 Answers

FIrst store your data in NSString. And then convert your string to NSDate.

You can refer SO:

Converting NSString to NSDate (and back again)

NSString to NSDate conversion problem

How to convert NSString to NSDate using NSDateFormatter?

like image 105
Rushi Avatar answered Sep 30 '22 09:09

Rushi


Convert NSDate to NSString and try to encode.

- (NSString *) changeDateToDateString :(NSDate *) date {      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];     [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];     NSLocale *locale = [NSLocale currentLocale];     NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"hh mm" options:0 locale:locale];     [dateFormatter setDateFormat:dateFormat];     [dateFormatter setLocale:locale];     NSString *dateString = [dateFormatter stringFromDate:date];     return dateString; } 
like image 24
pradeepa Avatar answered Sep 30 '22 09:09

pradeepa