得到异常:NSInvalidArgumentException

得到异常:NSInvalidArgumentException,这是在我json编码NSDate对象时出现的,我觉得是NSDate不兼容JSON编码,但是必须编码这个日期,不知道怎么办?

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSDate)'

JSON只支持一些特定的object, 基本上就是字符串跟数字.
NSDate当然不行,但是你可以用一串字符表示一个Date啊, 或者用数字表示Date.

NSDate *theDate = [NSDate date];
NSString *dateString = [NSString stringWithFormat:@"%f", [theDate timeIntervalSince1970]];
NSNumber *dateNumber = [NSNumber numberWithDouble:[theDate timeIntervalSince1970]];

这样,NSString and NSNumber 都是合法的可以转换成JSON data的.