从复杂的NSString中提取日期

Mar*_*coz 1 string date objective-c nsstring ios

我没有Xcode的能力来解决这个问题:

我有这个文字:

"402 Garcia 01/08/15 10:26 Observacionesdelhuésped"

我想提取我确定GMT + 0的日期,然后添加手机GMT例如GMT +1并将旧日期替换为NSString中的新日期.

GMT的东西,我刚刚在另一个地方解决它,所以我只需要提取并将日期字符串替换为字符串,所以我的最终结果将是这样的:

"402 Garcia 01/08/15 11:26 Observacionesdelhuésped"

任何帮助将不胜感激,并提前感谢.

HAS*_*HAS 6

这正是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;
}
Run Code Online (Sandbox Code Playgroud)

你可以像这样调用它:

NSArray *dates = [@"402 Garcia 01/08/15 10:26 Observaciones del huésped" detectedDates];
Run Code Online (Sandbox Code Playgroud)

你可以阅读更多关于NSDataDetector超过NSHipster