string - Swift String.removeRange 无法编译

标签 string swift

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

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

相关文章:

c - 自由(): invalid next size (fast) string too long?

java - 空字符串对象和字符串文字的串联

php - 快速以肖像模式拍摄照片并上传到服务器 - 上传的图像显示方向错误

swift - 如何检查一个函数是否总是返回一个值(也就是 "doesn' t fall off the end”)?

iOS 支持外接键盘而不显示

arrays - 将字符串拆分为 n 个子字符串的可能方法

ios - 不需要的信息显示在 NSString 的文本标签中

ListView 中的Android BitmapDecode

Swift 将 GestureRecognizer 添加到 UIStackView 之上的 View

JSON后台处理和本地通知