2012-01-07 68 views

回答

2

苹果在10.7中包括NSDataDetector
除了URL,电话号码等,它还支持日期(NSTextCheckingTypeDate)检测。 (似乎Mail.app广泛使用的探测器)

这个例子检测“串”的所有日期和记录比赛(如果有的话)的位置&长度:

NSError* error = NULL; 
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeDate error:&error]; 
NSArray* matches = [detector matchesInString:string options:0 range:NSMakeRange(0, [string length])]; 
for (NSTextCheckingResult* match in matches) 
{ 
    NSRange matchRange = [match range]; 
    NSLog(@"Match at position:%lu with length:%lu", matchRange.location, matchRange.length); 
} 
+0

很好的回答(和+ 1),但是Kentzo也在寻找一个运行在10.6以下的解决方案。 – 2012-01-07 13:52:38

+0

谢谢,错过了。但我仍然需要在10.6中提取日期,是否有更天真的解决方案(例如基于NSScanner或NSDateFormatter)? – Kentzo 2012-01-07 14:15:54

相关问题