這是我的代碼:
let textRange = UETextRange(charIndex: i, length: l - i)
for j in textRange.charIndex...getCharIndexAfterEndOfRange(range: textRange) {
if String(initialText[j - 1]) == "\n" { //error here
hits = 1
let hitIndex = attributeRange.index (hits - 1)
if hitIndex <= paragraphStyles.count {
let charIndex = paragraphStyles[hitIndex].charIndex
}
}
}
我該如何解決這個錯誤?
uj5u.com熱心網友回復:
String在 Swift 中訪問 a 的元素與大多數語言略有不同。Swift 的String下標需要一個String.Index(不是Int),如檔案中所示:
@inlinable public subscript(i: String.Index) -> Character { get }
我們想要做的是獲取startIndex, 偏移我們想要的整數索引,然后從字串中獲取該索引。固定代碼:
let char = initialText[initialText.index(initialText.startIndex, offsetBy: j - 1)]
if char == "\n" {
/* ... */
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/366478.html
上一篇:我如何遍歷字串中的每個string.gmatch然后用它的位元組碼版本替換匹配項?
下一篇:從數字、符號和其他語言中清除句子
