如何使用NSRange从字符串中提取特定句子?

时间:2016-03-29 14:50:57

标签: ios string swift swift2 nsrange

这是我的代码:

regex.enumerateMatchesInString(parsingMemo.string, options: NSMatchingOptions(rawValue: 0), range: NSMakeRange(0, parsingMemo.length), usingBlock: { (match: NSTextCheckingResult?,_, _) -> Void in
let trange = match!.rangeAtIndex(0)
let range = Range(start: trange.location, end: trange.length)
var insideString: String = parsingMemo.string.substringWithRange(NSMakeRange(trange.location,trange.length) 

代码给出了以下错误:

  

无法转换类型的值' NSRange' (又名' _NSRange')预期参数类型'范围' (又名'范围')

如何修复我的代码?

1 个答案:

答案 0 :(得分:0)

您需要将您获得的NSRange转换为Swift Range。这样的事情应该有效:

let start = parsingMemo.string.characters.startIndex.advancedBy(trange.location)
let end = start.advancedBy(trange.length)

var insideString: String = parsingMemo.string.substringWithRange(start..<end)