我在 Apple Dev 论坛https://devforums.apple.com/message/956514上发布了这个问题。我希望它也可以在这里发布。Apple 开发者论坛上还没有回复。
每当我输入小于 1900 年的日期时,NSDataDetector 都会匹配错误的日期。
例如:
1201 年 1 月 1 日匹配到 2015 年 1 月 1 日和 1201 年 2 月 1 日匹配到 2014 年 2 月 1 日
似乎早于 1900 年的 1 月份的任何日期都会返回 1 月份的那个日期,但 2015 年。任何晚于 1 月但 1900 年之前的任何年份的日期都与 2014 年的那个日期匹配。
这是我检测日期的代码:
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSArray *matches = [detector matchesInString:myDateString options:0 range:NSMakeRange(0, [myDateString length])];
for (NSTextCheckingResult *match in matches) {
if (match.resultType == NSTextCheckingTypeDate) {
determinedDate = match.date;
}
}
myDateString 只是一个包含需要解析的日期值的字符串。
我做错了什么还是这是 NSDataDetector 的一个已知问题?