我不明白如何处理编译器报告的问题。我试图创建一个范围,但它说索引未知:
//let range = matches.first!.range.location
let range = Range(
start:matches.first!.range.location,
end: matches.first!.range.location+matches.first!.range.length
)
id = text[range]
var t = text
t.removeRange(range)
return t
编译器说:无法在 t.removeRange(range) 上使用类型为“(Range)”的参数列表调用“removeRange”。 我很确定这很明显,但我在这么小的问题上浪费了很多时间……非常感谢任何帮助!
最佳答案
正如您的错误所述:
Cannot invoke 'removeRange' with an argument list of type '(Range)'
表示你的range
有问题实例类型和 removeRange
函数将只接受类型为 Range<String.Index>
的参数它的语法是:
/// Remove the indicated `subRange` of characters
///
/// Invalidates all indices with respect to `self`.
///
/// Complexity: O(\ `count(self)`\ ).
mutating func removeRange(subRange: Range<String.Index>)
这里是 removeRange
的工作示例:
var welcome = "hello there"
let range = advance(welcome.endIndex, -6)..<welcome.endIndex
welcome.removeRange(range)
println(welcome) //hello
希望这会有所帮助。
关于string - Swift String.removeRange 无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31753349/