NSDateFormatter still parsing instead having incorrect format

后端 未结 3 659
北荒
北荒 2020-12-07 02:11

Having some problems parsing date. I have an array of supported formats and once I receive the date (string) from API, I try to parse it iterating through the formats until

3条回答
  •  余生分开走
    2020-12-07 03:01

    Had a similar issue:

    NSDateFormatter returns date object from invalid input

    Filed a bug report at Apple. Result: Will not be fixed, as the change could break working code, in addition it is more error tolerant and thus provides some kind of convenience.

    Please know that our engineering team has determined that this issue behaves as intended based on the information provided.

    It appears that ICU’s udat_parseCalendar() is very lenient and still is able to parse even if the string doesn’t exactly match the format. We understand preferring that the formatter return nil in these cases but (1) there’s no easy way for us to know that the input string doesn’t match the format since ICU allows it and doesn’t throw an error and (2) suddenly returning nil in these cases would almost certainly be a bincompat issue.

    In my case I had the option to either modify the unit tests and be more tolerant in case of invalid input or have an additional checkup (based on the recommended approach, which is the accepted answer for the post) whether the resulting NSDate's string fits to the input string.

提交回复
热议问题