我正在尝试将此字符串 @"August 8, 2013" 转换为 NSDate 但我的结果一直为 NULL:
strToConvert = @"August 8, 2013";
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/YYYY zzz"];
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
我的格式不正确吗?
编辑:
我将方法更改为:
- (NSDate*) convertStringToDate : (NSString*) strToConvert {
NSError* error = nil;
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
NSArray* arrDateMatches = [detector matchesInString:strToConvert options:0 range:NSMakeRange(0, [strToConvert length])];
for (NSTextCheckingResult* match in arrDateMatches) {
strToConvert = [self convertDateToString:match.date];
}
NSDate* myDate = [dateFormatter dateFromString:strToConvert];
return myDate;
}
它工作正常。唯一的问题是我从枚举类型'enum NSTextCheckingType'中得到一个隐式转换......关于如何修复该警告的任何想法?