设置NSDataDetector的上下文日期

and*_*ros 8 objective-c nsdate ios nsdatadetector

假设今天是2014年1月20日.如果我使用NSDataDetector从字符串"明天下午4点"中提取日期,我将得到2014-01-21T16:00.大.

但是,假设我希望NSDataDetector 假装当前日期是2014年1月14日.这样,当我解析"明天下午4点"时,我会得到2014-01-15T16:00.如果我在设备上更改系统时间,我会得到我想要的.但是,有没有办法以编程方式指定它?

谢谢.

Phi*_*ade 2

出于测试目的,您可以使用一种称为method swizzling的技术。诀窍是用NSDate您自己的方法之一替换 的方法之一。

如果您替换+[NSDate date]为您自己的实现,NSDataDetector则会将“现在”视为您指定的任何时间。

在生产代码中混合系统类方法是有风险的。以下示例代码通过利用知道它是私有的来忽略封装。许多潜在的陷阱之一是,如果 iOS 的下一次更新更改了 NSDataDetector 的内部结构,您的生产应用程序可能会意外地停止为最终用户正常工作NSDataDetectorNSDate

添加一个类别,NSDate如下所示(顺便说一句:如果您正在构建要在设备上运行的库,您可能需要指定 -all_load 链接器标志以从库加载类别):

#include <objc/runtime.h>

 @implementation NSDate(freezeDate)

static NSDate *_freezeDate;

// Freeze NSDate to a point in time.
// PROBABLY NOT A GOOD IDEA FOR PRODUCTION CODE
+(void)freezeToDate:(NSDate*)date
{
    if(_freezeDate != nil) [NSDate unfreeze];
    _freezeDate = date;
    Method _original_date_method = class_getClassMethod([NSDate class], @selector(date));
    Method _fake_date_method = class_getClassMethod([self class], @selector(fakeDate));
    method_exchangeImplementations(_original_date_method, _fake_date_method);
}

// Unfreeze NSDate so that now will really be now.
+ (void)unfreeze
{
    if(_freezeDate == nil) return;
    _freezeDate = nil;
    Method _original_date_method = class_getClassMethod([NSDate class], @selector(date));
    Method _fake_date_method = class_getClassMethod([self class], @selector(fakeDate));
    method_exchangeImplementations(_original_date_method, _fake_date_method);
}

+ (NSDate *)fakeDate
{
    return _freezeDate;
}

@end
Run Code Online (Sandbox Code Playgroud)

这是它的使用情况:

- (void)someTestingFunction:(NSNotification *)aNotification
{
    // Set date to be frozen at a point one week ago from now.
    [NSDate freezeToDate:[NSDate dateWithTimeIntervalSinceNow:(-3600*24*7)]];

    NSString *userInput = @"tomorrow at 7pm";
    NSError *error = nil;
    NSRange range = NSMakeRange(0, userInput.length);
    NSDataDetector *dd = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error];
    [dd enumerateMatchesInString:userInput
                         options:0
                           range:range
                      usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
                          NSLog(@"From one week ago: %@", match);
                      }];

    // Return date to normal
    [NSDate unfreeze];

    [dd enumerateMatchesInString:userInput
                         options:0
                           range:range
                      usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
                          NSLog(@"From now: %@", match);
                      }];

}
Run Code Online (Sandbox Code Playgroud)

哪个输出:

2014-01-20 19:35:57.525 TestObjectiveC2[6167:303] 一周前:{0, 15}{2014-01-15 03:00:00 +0000}
2014-01-20 19:35:57.526 TestObjectiveC2[6167:303] 从现在开始:{0, 15}{2014-01-22 03:00:00 +0000}