當我嘗試從較大的字符串中提取字符串時,它給了我一個範圍或索引超出範圍的錯誤。我可能忽略了一些非常明顯的東西。謝謝。使用substringWithRange提取字符串:給出「索引超出範圍」
NSString *title = [TBXML textForElement:title1];
TBXMLElement * description1 = [TBXML childElementNamed:@"description" parentElement:item1];
NSString *description = [TBXML textForElement:description1];
NSMutableString *des1 = [NSMutableString stringWithString:description];
//search for <pre> tag for its location in the string
NSRange match;
NSRange match1;
match = [des1 rangeOfString: @"<pre>"];
match1 = [des1 rangeOfString: @"</pre>"];
NSLog(@"%i,%i",match.location,match1.location);
NSString *newDes = [des1 substringWithRange: NSMakeRange (match.location+5, match1.location-1)]; //<---This is the line causing the error
NSLog(@"title=%@",title);
NSLog(@"description=%@",newDes);
UPDATE:該範圍的第二部分是一個長度,而不是端點。 D'哦!
你應該把你的「解決方案」作爲回答,並接受它。 – LucasTizma 2011-05-18 23:45:06