- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设今天是 2014 年 1 月 20 日。如果我使用 NSDataDetector 从字符串 “明天下午 4 点” 中提取日期,我将得到 2014-01-21T16:00。太好了。
但是,假设我希望 NSDataDetector 假装当前日期是 2014 年 1 月 14 日。这样,当我解析“明天下午 4 点”时,我将得到 2014-01-15T16:00。如果我更改设备上的系统时间,我会得到我想要的。但是,有没有办法以编程方式指定它?
谢谢。
最佳答案
出于测试目的,您可以使用一种称为 method swizzling 的技术.诀窍是用您自己的方法替换 NSDate
的方法之一。
如果您将 +[NSDate date]
替换为您自己的实现,NSDataDetector
会将“现在”视为您指定的任何时间。
在生产代码中调配系统类方法是有风险的。以下示例代码忽略了 Encapsulation NSDataDetector
利用知道它私下使用 NSDate
的优势。许多潜在的陷阱之一是,如果 iOS 的下一次更新更改了 NSDataDetector 的内部结构,您的生产应用可能会意外地停止为您的最终用户正常工作。
像这样将类别添加到 NSDate
(顺便说一句:如果您正在构建要在设备上运行的库,you may need to specify the -all_load linker flag 以从库中加载类别):
#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
下面是它的使用:
- (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);
}];
}
哪些输出:
2014-01-20 19:35:57.525 TestObjectiveC2[6167:303] From one week ago: {0, 15}{2014-01-15 03:00:00 +0000}2014-01-20 19:35:57.526 TestObjectiveC2[6167:303] From now: {0, 15}{2014-01-22 03:00:00 +0000}
关于ios - 为 NSDataDetector 设置上下文日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244144/
NSDataDetector 可以解析的表达式范围是否已记录? 我们知道 NSDataDetector 可以解释很多表达式,例如“星期一晚上”。可以理解的是,NSDataDetector 类引用不想详
我正在使用 NSDataDetector 来解析文本并检索数字。这是我的代码: NSDataDetector *detector = [NSDataDetector dataDetectorWith
我正在尝试使用 NSDataDetector 来验证电话号码。它似乎没有用,这可能意味着我用错了,但我不知道怎么用。下面代码中的所有这些电话号码都应该失败,第一个全为零,其余的部分中有太多数字。然而他
一段时间以来,我一直在努力理解 NSDataDetector 类。我已经阅读了文档,但无法理解。我对 Xcode 所做的一切都告诉我有错误。 我觉得这次尝试可能是在正确的道路上。如何编写一个简单的基础
我正在使用 NSDataDetector 扫描文本中的链接,然后返回它们。我已经让它适用于常规字母表,但由于我住在一个对英语字母表进行了一些补充的国家/地区,所以我无法使用 NSDataDetecto
我正在尝试将这个有效的 Objective C 代码转换为 Swift (which is based on this Apple Documentation) -(BOOL)validatePhon
我正在尝试移植一些 Obj-c 代码,但在创建 NSDataDetector 时遇到了一些问题。 在 Objective-C 中我会这样做: NSDataDetector *linkDetector
我目前正在使用 NSDataDetectors 来查找链接: -(void)setBodyText { NSString* labelText = [[message valueForKey:
如何使用 NSDataDetector 验证电话号码。在我的项目中,任何高于 14 的文本长度也是有效的,但 NSDataDetector 不会以这种方式检测它。 我正在使用以下 stackoverf
我有一个来自服务器的字符串,我想检查它是否包含电话号码、邮件地址和电子邮件等表达式。我在电话号码和邮件地址的情况下取得了成功,但没有电子邮件。为此,我正在使用 NSDataDetector。例如 NS
假设今天是 2014 年 1 月 20 日。如果我使用 NSDataDetector 从字符串 “明天下午 4 点” 中提取日期,我将得到 2014-01-21T16:00。太好了。 但是,假设我希望
在测试 Cocoa 数据检测器时,我发现没有检测到 forecast.io,而是检测到了 www.forecast.io 和 forecast.com是。 NSDataDetector 找到的所有顶级
我正在将一项功能从 iOS 应用程序移植到 Windows 10 UWP 应用程序,该应用程序需要在字符串中检测 URL(以便我可以在 View 中将 URL 部分显示为可点击)。在 iOS 中,NS
它不是那么重要,但如果我使用像 here 这样的 NSDataDetector并将其用于网络链接,我可以显示所有带有 .com、.us 甚至 .ca 的网站,但不能显示 .io如果我使用 http:/
我试图从 UNKNOWN 格式的 NSString 中获取 NSDate,所以我编写了如下函数 -(void)dateFromString:(NSString*)string { NSErro
我正在使用 NSDataDetector 读取 NSString 日期/时间并将其交换为 NSDate。 输入 (lastRefreshStampString) 是 2014-05-05 02:54:
我正在尝试为 UITextView 中的内容创建自定义数据检测器。我希望能够做这样的事情: tv.dataDetectorTypes = UIDataDetectorTypeAll; 但我想使用基于以
有没有办法访问Cocoa的NSDataDetector来自react-native? 类似UITextView的dataDetectorTypes? 我想用它进行自动地址检测,链接到 map 。 最佳
好的,我使用 NSDataDetector 来检测这样的电话号码: func matchesFor(type: NSTextCheckingResult.CheckingType) -> [Strin
在 iOS 中有一个名为 NSDataDetector 的类,例如,让您传递一个文本,它会检测该文本是否包含邮政地址、日期、链接等内容... 就我而言,我有兴趣为 Android 寻找一个类似的工具,
我是一名优秀的程序员,十分优秀!