简体   繁体   English

获取堆栈错误,原因不明

[英]Getting Stack Error, Unexplained

I have this code 我有这个代码

-(IBAction) generateFlashNow:(id)sender{

[textField resignFirstResponder];
NSString *string1 = textField.text;
//NSString *string2 = [string1 stringByReplacingOccurrencesOfString:@"" withString:@","];
NSArray *arrayOfLetters = [string1 componentsSeparatedByString:@","];

NSLog(@"Log Array :%@", arrayOfLetters);

//NSArray *imageArray = [[NSArray alloc] init];

NSLog(@"Log First Letter of array: %@",[arrayOfLetters objectAtIndex:0]);

runLoopTimes = [arrayOfLetters count];
runLoopTimes = (runLoopTimes -1);
NSLog(@"Letters:%d", runLoopTimes);

int timesThroughLoop;

while (runLoopTimes > 0) {
    NSLog(@"loopedy Loop");

    NSString *LetterString = [NSString stringWithFormat:@"%@", [arrayOfLetters objectAtIndex:indexTimes]];
    timesThroughLoop --;
    NSLog(@"letter String : %@", LetterString);
    indexTimes ++;

    if (runLoopTimes == 0) {
        return; 
    }
}

NSLog(@"done");


}

and i get this log report, when I type into the text field 's,a,m' 当我在文本字段“ s,a,m”中键入内容时,我得到此日志报告

[Session started at 2010-01-15 15:50:30 +1300.]
2010-01-15 15:50:35.398 Flash![39290:207] Log Array :(
s,
a,
m

) 2010-01-15 15:50:35.399 Flash![39290:207] Log First Letter of array: s 2010-01-15 15:50:35.399 Flash![39290:207] Letters:2 2010-01-15 15:50:35.400 Flash![39290:207] loopedy Loop 2010-01-15 15:50:35.400 Flash![39290:207] letter String : s 2010-01-15 15:50:35.401 Flash![39290:207] loopedy Loop 2010-01-15 15:50:35.401 Flash![39290:207] letter String : a 2010-01-15 15:50:35.402 Flash![39290:207] loopedy Loop 2010-01-15 15:50:35.402 Flash![39290:207] letter String : m 2010-01-15 15:50:35.404 Flash![39290:207] loopedy Loop 2010-01-15 15:50:35.405 Flash![39290:207] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSCFArray objectAtIndex:]: index (3) beyond bounds (3)' 2010-01-15 15:50:35.406 Flash![39290:207] Stack: ( 29287515, 2538743049, 29371451, 29371290, 478409, 74279, 9945, 2716761, 3124130, 3132867, 3128079, 2821683, 2730012, 2756789, 37383889, 29072256, 29068360, 37377933, 37378130, 2760707, 9228, 9082 ) )2010-01-15 15:50:35.399 Flash![39290:207]记录数组的第一个字母:s 2010-01-15 15:50:35.399 Flash![39290:207] Letters:2 2010-01-15 15:50:35.400 Flash![39290:207] loopedy循环2010-01-15 15:50:35.400 Flash![39290:207]字母字符串:s 2010-01-15 15:50:35.401 Flash![39290: 207] loopedy Loop 2010-01-15 15:50:35.401 Flash![39290:207] letter字符串:a 2010-01-15 15:50:35.402 Flash![39290:207] loopedy Loop 2010-01-15 15 :50:35.402 Flash![39290:207]字母字符串:m 2010-01-15 15:50:35.404 Flash![39290:207] loopedy Loop 2010-01-15 15:50:35.405 Flash![39290:207 ] *由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* -[NSCFArray objectAtIndex:]:超出范围(3)的索引(3)'2010-01-15 15:50:35.406 Flash![39290:207]堆栈:(29287515,2538743049,29371451,29371290,478409,74279,9945,2716761,3124130,3132867,3128079,2821683,2730012,2756789,37383889,29072256,29068360,37377933,37378130,2760707,9228,9082)

and I have no idea what I am doing wrong, so any help is appreciated. 而且我不知道我在做什么错,因此我们将不胜感激。

Cheers, Sam 干杯,山姆

You never decrement runLoopTimes inside the loop. 您永远不会减少runLoopTimes内的runLoopTimes So it will loop indefinitely. 因此它将无限期地循环。 And eventually indexTimes is off the end of the array. 最终indexTimes不在数组的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM