A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 haha92210 于 2014-11-28 17:26 编辑

- (int)ziMuCountInStr
{
    int count = 0;
    for (int i = 1; i <= self.length; i++) {
        unichar c = [self characterAtIndex:i];

        if (c >= 'a' && c <= 'z') {
            count++;
        }
    }
    return count;
}

这是输出得东西
2014-11-28 17:20:57.179 1128[27833:303] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds'
*** First throw call stack:
(
        0   CoreFoundation                      0x00007fff8ec6a25c __exceptionPreprocess + 172
        1   libobjc.A.dylib                     0x00007fff93614e75 objc_exception_throw + 43
        2   CoreFoundation                      0x00007fff8ec6a10c +[NSException raise:format:] + 204
        3   CoreFoundation                      0x00007fff8eb5178b -[__NSCFString characterAtIndex:] + 91
        4   1128                                0x000000010000174d -[NSString(MJ) ziMuCountInStr] + 93
        5   1128                                0x0000000100001a59 test3 + 57
        6   1128                                0x00000001000019a4 main + 36
        7   libdyld.dylib                       0x00007fff92f4e5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

2 个回复

倒序浏览
长度越界了i超过字符串长度了
回复 使用道具 举报
weizhang00 发表于 2014-11-28 17:33
长度越界了i超过字符串长度了

谢谢谢谢 莫名奇妙的觉得自己傻逼了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马