gpt4 book ai didi

string - Swift String.removeRange 无法编译

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:37 28 4
gpt4 key购买 nike

我不明白如何处理编译器报告的问题。我试图创建一个范围,但它说索引未知:

           //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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com