extension UITextVIEw { func separateDWordsInFrontCursor(infront: Bool = true) -> [String] { let cursorposition = selectedRange.location let separationCharacters = NSCharacterSet(charactersInString: " ") let beginRange = Range(start: text.startIndex.advancedBy(0),end: text.startIndex.advancedBy(cursorposition)) //Fatal error: can not increment endindex let endRange = Range(start: text.startIndex.advancedBy(cursorposition),end: text.startIndex.advancedBy(text.characters.count)) let beginPhrase = text.substringWithRange(beginRange) let endPhrase = text.substringWithRange(endRange) return infront ? beginPhrase.componentsSeparatedByCharactersInSet(separationCharacters) : endPhrase.componentsSeparatedByCharactersInSet(separationCharacters) }}
错误如下:
Fatal error: can not increment endindex
这是我尝试输入UITextVIEw:
UITextVIEw的键盘类型设置为“默认”.
出现错误时:
cursorposition is 3text.characters.count is 2text.startIndex is 0text.startIndex.advancedBy(2) is 3text.startIndex.advancedBy(cursorposition) is "Fatal error: can not increment endindex"
我该如何解决这个问题?
解决方法 正如@Martin R所说,我们需要对NSRange和Nsstring进行 *** 作,因此解决方案如下:private func separateDWordsInFrontCursor(infront: Bool = true) -> [String] { let cursorposition = selectedRange.location let separationCharacters = NSCharacterSet(charactersInString: " ") let nstext = Nsstring(string: text) let beginRange = NSRange(location: 0,length: cursorposition) let endRange = NSRange(location: cursorposition,length: nstext.length - cursorposition) let beginPhrase = nstext.substringWithRange(beginRange) let endPhrase = nstext.substringWithRange(endRange) return infront ? beginPhrase.componentsSeparatedByCharactersInSet(separationCharacters) : endPhrase.componentsSeparatedByCharactersInSet(separationCharacters)}总结
以上是内存溢出为你收集整理的ios – “无法递增endIndex” – 由于UITextView中的表情符号字符而出错全部内容,希望文章能够帮你解决ios – “无法递增endIndex” – 由于UITextView中的表情符号字符而出错所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)