I fail to understand the problem Xcode is confronting me with in this line:
iteration.template = template[iterationSubstring.endIndex...substring.startIndex]
template
is a String
and iterationSubstring
and substring
are Substring
s of template
. Xcode highlights the opening square bracket with the following message:
Subscript 'subscript(_:)' requires the types 'Substring.Index' and 'Int' be equivalent
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 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]
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
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…