开发者社区 问答 正文

得到异常:NSInvalidArgumentException

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

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

展开
收起
爵霸 2016-03-26 09:25:25 2223 分享 版权
1 条回答
写回答
取消 提交回答
  • 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的.

    2019-07-17 19:15:18
    赞同 展开评论
问答分类:
问答地址: