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)'
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?
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With