簡體   English   中英

從Objective-C中的描述字符串獲取日期

[英]Get date from description string in Objective-C

我想知道是否有一種方法可以從包含一些其他信息的字符串中查找和轉換日期:

例如,“您的司機將在星期一到這里”“ 11月24日到場”

為了從此信息創建一個NSDate。

感謝大伙們 !

請嘗試以下功能:

-(void)fetchDateFromString:(NSString*)stringObj
{

    NSError *error = NULL;
    NSDataDetector *detectorObj = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];

    NSArray *matchesObj = [detectorObj matchesInString:stringObj
                                         options:0
                                           range:NSMakeRange(0, [stringObj length])];

    NSLocale* currentLoc = [NSLocale currentLocale];
    for (NSTextCheckingResult *match in matchesObj) {
        if ([match resultType] == NSTextCheckingTypeDate) {
            NSLog(@"Date : %@", [[match date] descriptionWithLocale:currentLoc]);
        }
    }
}

並如下調用:

[self fetchDateFromString:@"Be present the November 24th"];

祝好運.....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM