ios - Swift "can not increment endIndex" when deleting Japanese characters -
i'm using swift 2.0/xcode 7.0 ios app. i've built japanese ime convert roman characters japanese equivalent. example:
- typing "a" convert "a" "あ"
- typing "k" nothing (no match in japanese) until next character typed, example typing "k" "a" result in "か"
my problem when try delete japanese character. if there 1 character in text field, delete key/function works expected. however, if there more one, when try delete character, receive can not increment endindex
error in following code.
var imeinputlength: int = 0 let currentinputvalue: string = txtfldyourresponse.text!.lowercasestring if(currentinputvalue.characters.count==0) { imeinputlength = 0 } let inputstringtokeep: string = currentinputvalue.substringwithrange( range<string.index>(start: currentinputvalue.startindex.advancedby(imeinputlength), end: currentinputvalue.endindex)) let imestringtokeep: string = currentinputvalue.substringwithrange( range<string.index>(start: currentinputvalue.startindex, end: currentinputvalue.startindex.advancedby(imeinputlength))) if let imevaluedc = jimedc[inputstringtokeep] { txtfldyourresponse.text = imestringtokeep + imevaluedc imeinputlength = (txtfldyourresponse.text?.characters.count)!-1 } if let imevalue = jime[inputstringtokeep] { txtfldyourresponse.text = imestringtokeep + imevalue imeinputlength = txtfldyourresponse.text!.characters.count }
currentinputvalue
text text field.
imeinputlength
int
(initial value = 0) increments total character count in text field after match found.
jimedc
, jime
key/value pairs handle conversion roman japanese characters.
i'm printing endindex
console before code runs. appears increment/decrement expected, code block above fails increment error.
i've been banging head against couple weeks no progress.
edit: clarified handling of imeinputlength
, added additional code clarification.
a side conversation l'l'l got me on right track. confused error message can not increment endindex
. in reality, creating out-of-bounds error on range.
when backspace key pressed, wasn't modifying imeinputlength
reflect new length of input string. result, advanceby
amount larger updated endindex
value. swift threw can not increment endindex
reason startindex
greater endindex
.
i resolved first checking following:
if(imeinputlength > currentinputvalue.characters.count) { print("you must have pressed backspace") imeinputlength = txtfldyourresponse.text!.characters.count } else { let inputstringtokeep: ... }
if backspace key pressed, needed see if imeinputlength
value greater current character count of input field. use case imeinputlength
ends greater when japanese character deleted using backspace key. when condition caught, reset imeinputlength
current character count , exit routine.
i'm happy of use cases test out positive!
Comments
Post a Comment