0

したがって、ユーザーが画面に沿って指をパンすると、青い背景色を の属性付きテキストに動的に設定しようとしていますが、次のエラーメッセージが表示UILabelされる理由を一生理解できませんNSRangeExceptionTerminating 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 に答える