【发布时间】:2014-06-21 18:40:32
【问题描述】:
我有这段代码在 Objective-C 中工作
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"(.*?)(<[^>]+>|\\Z)" options:NSRegularExpressionCaseInsensitive|NSRegularExpressionDotMatchesLineSeparators error:nil];
NSArray* results = [regex matchesInString:text options:NSMatchingReportProgress range:NSMakeRange(0, text.length)];
但是 Swift 版本不工作。 matchesInString 返回一个空的 Array(在两种情况下都使用相同的标记文本)
let regexOptions = NSRegularExpressionOptions.CaseInsensitive | NSRegularExpressionOptions.DotMatchesLineSeparators
let regex = NSRegularExpression.regularExpressionWithPattern("(.*?)(<[^>]+>|\\Z)", options: regexOptions, error: nil)
var results = regex.matchesInString(markupText, options: nil, range: NSMakeRange(0, countElements(markupText))) as Array<NSTextCheckingResult>
尽管文档说明matchesInString 返回NSTextCheckingResult 的Array,但我注意到在Objective-C 代码(有效)中Array 包含NSSimpleRegularExpressionCheckingResult 对象而不是NSTextCheckingResult对象。 Swift 版本中报告的空 Array 报告了零个 NSTextCheckingResult 对象
知道这里缺少什么吗?
【问题讨论】: