繁体   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