2012-01-07 64 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

相關問題