[英]How do I extract a particular sentence from a string using NSRange?
This is my code: 这是我的代码:
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)
The code gives me the following error: 该代码给我以下错误:
Cannot convert value of type 'NSRange' (aka '_NSRange') to expected argument type 'Range' (aka 'Range')
无法将类型“ NSRange”(也称为“ _NSRange”)的值转换为预期的参数类型“ Range”(也称为“ Range”)
How can I fix my code? 如何修复我的代码?
You need to convert the NSRange
that you were given into a Swift Range
. 您需要将获得的
NSRange
转换为Swift Range
。 Something like this should work: 这样的事情应该起作用:
let start = parsingMemo.string.characters.startIndex.advancedBy(trange.location)
let end = start.advancedBy(trange.length)
var insideString: String = parsingMemo.string.substringWithRange(start..<end)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.