App crashes often in a for-loop

Posted by Flocked on Stack Overflow See other posts from Stack Overflow or by Flocked
Published on 2010-03-22T02:41:58Z Indexed on 2010/03/22 2:51 UTC
Read the original article Hit count: 444

Filed under:
|
|

Hello, my app crashes often in this for-loop:

for (int a = 0; a <= 20; a++) {
        int b = arc4random() % [newsStories count];
        NSString * foo = [[NSString alloc]initWithString:[[newsStories objectAtIndex: arc4random() % b]objectForKey:@"title"]];
        foo = [foo stringByReplacingOccurrencesOfString:@"’" withString:@""];
        foo = [[foo componentsSeparatedByCharactersInSet:[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:@" "];
        foo = [foo stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        textView.text = [textView.text stringByAppendingString:[NSString stringWithFormat:@"%@ ", foo]];
    }

The code changes a NSString from a NSDictionary and adds it into a textView. Sometimes it first crashes in the second time using the for-loop. What did I wrong?

© Stack Overflow or respective owner

Related posts about nsstring

Related posts about for-loop