0

因此,当用户在屏幕上平移手指时,我尝试为 aUILabel的属性文本动态设置蓝色背景色,但我终其一生都无法弄清楚为什么我会收到NSRangeException以下错误消息:Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds有时。

这是我的代码:

var afterContext = ""                    
    if textDocumentProxy.documentContextAfterInput != nil {
       afterContext = textDocumentProxy.documentContextAfterInput!
    } else {
       afterContext = ""
    }

    if beforeContext != nil {              
        if initialContext == nil {                                         
            myRange = NSRange(location: 0, length:afterContext.characters.count)

           let attrString = NSMutableAttributedString(string: selectedTextView.text!)
           attrString.addAttribute(NSBackgroundColorAttributeName, value: UIColor(red: 20/255, green: 111/255, blue: 225/255, alpha: 1), range: myRange)
        }
     }
4

1 回答 1

0

这个简单的 if 条件似乎已经永久修复了它

if afterContext.characters.count > 0 {
   myRange = NSRange(location: 0, length: afterContext.characters.count-1)
} else {
   myRange = NSRange(location: 0, length: afterContext.characters.count)
}
于 2015-12-08T14:19:32.793 回答