运行代码时出现此错误 2013-02-23 10:52:54.063 Calculator[31319:11303] * 由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'-[__NSCFString characterAtIndex:]: Range or index out of bounds' * First throw call stack: (0x1c90012 0x10cde7e 0x1c8fdeb 0x1c56c0d 0x2d4b 0x10e1705 0x18920 0x188b8 0xd9671 0xd9bcf 0xd8d38 0x4833f 0x48552 0x263aa 0x17cf8 0x1bebdf9 0x1bebad0 0x1c05bf5 0x1c05962 0x1c36bb6 0x1c35f44 0x1c35e1b 0x1bea7e3 0x1bea668 0x1565c 0x22c2 0x21f5 0x1) My application usually takes the string entered in the textbox and转换为 ascii 值,然后我为它编写了一个小算法。下面是我如何将字符串转换为 ascii 的一段代码
for (int i=0; i<[first length]; i++) {
unichar ch = [first characterAtIndex:i];
firsttotal = firsttotal +ch;
}
for (int j=0; j<[second length]; j++) {
unichar chi = [first characterAtIndex:j];
secondtotal = secondtotal +chi;
}