这里在我的代码我试图加载一个ROBOTS.TXT文件,然后通过“Disallow”字符串进行过滤,然后如果该行有“Disallow string”,请删除此字符串并返回其余行。为什么我从NSString中删除字符时出错?
这里是robots.txt的
User-agent: *
Disallow:/search
Disallow: /groups
Disallow: /images
Disallow: /catalogs
Allow: /catalogs/about
Disallow: /catalogues
Disallow: /news
Allow: /news/directory
的样品,在这里的是,我的代码:
NSRange range;
NSURL *url=[NSURL URLWithString:@"http://www.google.com/robots.txt"];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSArray *parsed = [content componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSMutableArray *robots=[[[NSMutableArray alloc] init] autorelease];
NSMutableString *line= [[[NSMutableString alloc] init] autorelease];
for(line in parsed){
range=[line rangeOfString:@"Disallow"];
if (range.location != NSNotFound) {
[line deleteCharactersInRange: NSMakeRange(1,9)];
[robots addObject:line];
}
}
和这里是错误:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to mutate immutable object with deleteCharactersInRange:'
注意'[[[NSMutableArray里的alloc]初始化]自动释放]'可以更清楚和简单地写:'[NSMutableArray的阵列]'的方便的方法可以帮助编写干净的代码,它们可以用于ARC和非ARC代码。 – zaph
http://stackoverflow.com/questions/1242335/categories-for-nsmutablestring-and-nsstring-causing-binding-confusion这个问题可能与你的相关,并可能回答你的问题。希望它可以帮助 – BWC