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

前端 未结 8 2160
南旧
南旧 2021-02-04 01:05

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?



        
8条回答
  •  无人及你
    2021-02-04 01:25

    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;
    }
    

提交回复
热议问题