我没有能力在Xcode中解决这个问题:
我有一条短信:
"402 Garcia 01/08/15 10:26个胡须观测站“
我想提取的日期,我肯定是格林尼治标准时间+0,然后添加电话格林尼治时间,例如格林尼治时间+1,并将旧日期替换为新的日期在NSString内。
我刚刚在另一个地方解决了GMT的问题,所以我只需要提取并替换字符串中的日期字符串,这样我的最终结果将是:
"402 Garcia 01/08/15 11:26个胡须观测站“
如有任何帮助,请提前表示感谢。
发布于 2015-01-09 13:38:47
这正是NSDataDetector存在的目的。
我在NSString上创建了一个类别中的方法:
@interface NSString (HASAdditions)
- (NSArray *)detectedDates;
@end
@implementation NSString (HASAdditions)
- (NSArray *)detectedDates {
NSError *error = nil;
NSDataDetector *dateDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
if (!dateDetector) return nil;
NSArray *matches = [dateDetector matchesInString:self options:kNilOptions range:NSMakeRange(0, self.length)];
NSMutableArray *dates = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in matches) {
if (match.resultType == NSTextCheckingTypeDate) {
[dates addObject:match.date];
}
}
return dates.count ? [dates copy] : nil;
}你可以这样说:
NSArray *dates = [@"402 Garcia 01/08/15 10:26 Observaciones del huésped" detectedDates];您可以在NSDataDetector网上阅读更多关于NSHipster的内容。
发布于 2015-01-09 13:16:01
本作品始终是同一文本结构。
NSString *text = @"402 Garcia 01/08/15 10:26 Observaciones del huésped";
// This the formatter will be use.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yy HH:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
// First we extract the part of the text we need.
NSArray *array = [text componentsSeparatedByString:@" "];
NSString *dateString = [NSString stringWithFormat:@"%@ %@",[array objectAtIndex:2],[array objectAtIndex:3]];
// Here the search text
NSLog(@"%@",dateString);
// Now we use the formatter and the extracted text.
NSDate *date = [formatter dateFromString:dateString];
NSLog(@"The date is: %@",[date description]);https://stackoverflow.com/questions/27861182
复制相似问题