NSDateFormatter dateFromString在iOS 10上工作,但在iOS 11上返回nil
我的内部应用程序在过去两年中一直在运行,它有不同格式的NSDateFormatter dateFromString在iOS 10上工作,但在iOS 11上返回nil,ios,objective-c,nsdate,nsdateformatter,Ios,Objective C,Nsdate,Nsdateformatter,我的内部应用程序在过去两年中一直在运行,它有不同格式的消息和日期 1* Sep 20, 2017 at 8:09:39 PM 2* Feb 3, 2017, 5:49:42 PM 我将这些字符串转换为日期,如下所示 NSDateFormatter *df = [[NSDateFormatter alloc]init]; [df setDateStyle:NSDateFormatterMediumStyle]; [df setTimeStyle:NSDateFormatterMediumSty
消息和日期
1* Sep 20, 2017 at 8:09:39 PM
2* Feb 3, 2017, 5:49:42 PM
我将这些字符串转换为日期,如下所示
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterMediumStyle];
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
return [df dateFromString:dtStr];
对于date 1,它可以在iOS 10和iOS 11上完美运行
但是date 2只在iOS 10上运行,而不是在iOS 11上运行
我该如何解决这个问题呢?所以看起来你更大的问题实际上是你有多种日期格式 请参见此处的问题/答案: 它似乎没有那么有效。。。但这对你有用。正如其他人所评论的,它还使用特定的日期格式 以下是针对您的问题的经过调整的答案:
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *date1 = @"Sep 20, 2017 at 8:09:39 PM";
NSString *date2 = @"Feb 3, 2017, 5:49:42 PM";
NSLog(@"Date 1: %@", [self dateFromString:date1]);
NSLog(@"Date 2: %@", [self dateFromString:date2]);
}
- (NSDate *)dateFromString:(NSString *)dateString {
NSDate *date = nil;
// here, insert other date formats you know might be given
NSArray *dateFormatterList = @[
@"MMM d, y, hh:mm:ss a",
@"MMM d, y 'at' hh:mm:ss a"
];
if (dateString) {
for (NSString *dateFormatterString in dateFormatterList) {
[self.dateFormatter setDateFormat:dateFormatterString];
date = [self.dateFormatter dateFromString:dateString];
if (date) break;
}
}
return date;
}
- (NSDateFormatter *)dateFormatter {
if (!_dateFormatter) {
_dateFormatter = [[NSDateFormatter alloc] init];
}
return _dateFormatter;
}
@end
基本上正在发生的是。。。我们有一系列已知的日期格式。我们迭代每种格式,并尝试使用该格式从日期字符串获取NSDate。如果我们得到一个,我们会返回那个日期。如果有更多的日期格式,只需将其插入dateFormatterList。你会注意到我已经用了两种日期格式:@“mmd,y,hh:mm:ss a”
表示日期1*和上面日期2*的“mmd,y”表示“hh:mm:ss a”
我用你的两次约会测试了一下,效果很好。
日志:
日期1:2017年9月20日星期三20:09:39
日期2:2017年2月3日星期五17:49:42
请检查:
var dateStr = "Feb 3, 2017, 5:49:42 PM"
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSArray *matches = [detector matchesInString: dateStr options:0 range:NSMakeRange(0, [dateStr length])];
for (NSTextCheckingResult *match in matches) {
if (match.resultType == NSTextCheckingTypeDate) {
NSLog(@"%@", match.date);
}
}
Swift 4
var date = try? NSDataDetector(types: NSTextCheckingResult.CheckingType.date.rawValue)
.matches(in: dateStr, range: NSRange(location: 0, length: dateStr.count))
.flatMap{$0.date}
print(date!)
期望像nsDateFormatTermeLiumStyle这样的模糊格式能够像这样工作是非常懒惰和危险的。“如果你知道日期字符串的格式,为什么不使用与之匹配的特定格式的格式化程序?”马特同意,但有趣的是,该代码在iOS 10中工作,而不是在iOS 11中工作。我想知道苹果是否改变了这些操作系统版本之间的默认日期或时间格式?看起来很有可能。@Duncac也许他们有,但代码一开始就很傻;它或多或少是偶然工作的。问题是多人已经在这个项目上工作过,将日期固定为一种格式现在不起作用(这是以后的事情),因为已经存储在db中的消息具有不同的日期格式。。。我需要修复此问题以发布热修复程序……@S.J您误解了。您的解决方案是使用
日期格式
取代日期样式
和时间样式
。您必须尝试每种可能发送到应用程序的格式。