出于某种原因,我得到了
致命错误:无法递增endIndex
在线myRange。为什么我收到此错误?我不明白我的代码有什么问题。
<div class="class1 class2 class3 class4"></div>
<div class="class1 classb classv classd"></div>
<div class="class1 classw classdf classl"></div>
<div class="class1 classl classo classj"></div>
$('div.class1').each(function() {
console.log( $(this).attr('class').split(' ') )
})
答案 0 :(得分:6)
您的问题是decide
或decide+1
比字符串长。值得庆幸的是,有一些内置的代码来解决这个问题。
尝试替换
let myRange = Range<String.Index>(start: letter.startIndex.advancedBy(decide), end: letter.startIndex.advancedBy(decide+1))
currentLabel.text = letter.substringWithRange(myRange)
currentLetter = Character(letter.substringWithRange(myRange))
与
let end = letter.endIndex.advancedBy(-1, limit: letter.startIndex) //The -1 is actually required, as the 'limit:' bit does <=, not just <
let pos = letter.startIndex.advancedBy(decide, limit: end)
currentLetter = letter[pos]
currentLabel.text = currentLetter