比较目标c中的字符串

时间:2011-11-13 11:05:35

标签: objective-c ios

NSString *title = [myWebView 
                   stringByEvaluatingJavaScriptFromString:@"document.title"];
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:8];  
int j = 0;
int i=0;
int count=0;   
int len;
len = [title length];

for(i=0;i<len;i++){
    NSString *c = [title substringWithRange:NSMakeRange(i, 1)];

    if([c isEqualToString:@","])
    {
        //array[count]= [title substringWithRange:NSMakeRange(j, 2)];
        NSString *xxx = [title substringWithRange:NSMakeRange(j,(i-j))];
        NSLog(xxx);

        //insert the string into array
        [array insertObject:xxx atIndex:count];
        j=i;
        count = count + 1;
    }

}

我的应用总是崩溃

[c isEqualToString:@","]

并给出错误 - Thread1:程序接收信号:“SIGBART”。

我确信在比较字符串时问题正在发生,因为如果删除那一行代码,应用就会运行。

有人可以帮忙吗?感谢

2 个答案:

答案 0 :(得分:1)

考虑使用:

- (NSArray *)componentsSeparatedByString:(NSString *)separator

示例:

NSString *title = [myWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSMutableArray *array = [title componentsSeparatedByString:@","];  

答案 1 :(得分:0)

如果我的结尾长于字符串,或者未定义,那么你就会遇到问题。如果用1替换i,它不会崩溃,对吗?

顺便说一句,您可以使用:

unichar c = [title characterAtIndex:i];