为什么 String.subscript(_:) 在不涉及 `Int` 的情况下要求 `String.Index` 和 `Int` 类型相等? [英] Why does String.subscript(_:) require the types `String.Index` and `Int` to be equal when there is no `Int` involved?
问题描述
我无法理解 Xcode 在这一行中遇到的问题:
I fail to understand the problem Xcode is confronting me with in this line:
iteration.template = template[iterationSubstring.endIndex...substring.startIndex]
template
是 String
和 iterationSubstring
和 substring
是 Substring
<代码>模板代码>.Xcode 使用以下消息突出显示左方括号:
template
is a String
and iterationSubstring
and substring
are Substring
s of template
. Xcode highlights the opening square bracket with the following message:
下标 'subscript(_:)' 要求类型 'Substring.Index' 和 'Int' 是等价的
Subscript 'subscript(_:)' requires the types 'Substring.Index' and 'Int' be equivalent
错误信息对我来说没有任何意义.我尝试通过使用 [template.startIndex...template.endIndex]
下标创建 Range
来获取 Substring
.这与 Int
有什么关系?为什么同样的模式在其他地方也能用?
The error message does not make any sense to me. I try to obtain a Substring
by creating a Range<String.Index>
with the [template.startIndex...template.endIndex]
subscript. How is this related to Int
? And why does the same pattern work elsewhere?
重现问题的Xcode Playground 代码:
Xcode playground code reproducing the problem:
import Foundation
let template = "This is an ordinary string literal."
let firstSubstringStart = template.index(template.startIndex, offsetBy: 5)
let firstSubstringEnd = template.index(template.startIndex, offsetBy: 7)
let firstSubstring = template[firstSubstringStart...firstSubstringEnd]
let secondSubstringStart = template.index(template.startIndex, offsetBy: 10)
let secondSubstringEnd = template.index(template.startIndex, offsetBy: 12)
let secondSubstring = template[secondSubstringStart...secondSubstringEnd]
let part: String = template[firstSubstring.endIndex...secondSubstring.startIndex]
毕竟我有一个模板字符串和它的两个子字符串.我想得到一个 String
,范围从第一个 Substring
的结尾到第二个 Substring
的开头.
After all I have a template string and two substrings of it. I want to get a String
ranging from the end of the first Substring
to the start of the second Substring
.
推荐答案
当前版本的 Swift 与 Substring
结构体,它是一个切片的 String
.
The current version of Swift works with the Substring
struct which is a sliced String
.
该错误似乎具有误导性,如果您要将(范围下标的)Substring
分配给 String
变量,则会发生该错误.
The error seems to be misleading and occurs if you are going to assign a (range-subscripted) Substring
to a String
variable.
要修复错误,请从 Substring
iteration.template = String(template[iterationSubstring.endIndex...substring.startIndex])
尽管如此,强烈建议您不要使用来自不同字符串(iterationSubstring
和 substring
)的索引创建范围.对主字符串进行切片,保留索引.
Nevertheless you are strongly discouraged from creating ranges with indices from different strings (iterationSubstring
and substring
). Slice the main string, the indices are preserved.
第二个(同时删除)示例中的崩溃发生是因为字符串的最后一个字符在endIndex
之前的索引处,它是
The crash in the second (meanwhile deleted) example occurred because the last character of a string is at index before endIndex
, it's
template[template.startIndex..<template.endIndex]
或更短
template[template.startIndex...]
这篇关于为什么 String.subscript(_:) 在不涉及 `Int` 的情况下要求 `String.Index` 和 `Int` 类型相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!