我有一个关于数组中索引的严重问题。我一直在为此工作2天,但无法找到答案。替换两个字符串之间的字符串
我想这样做,搜索数组中的特定字符,然后将其替换为其他字符串。我正在使用replaceObjectAtIndex方法,但我的代码不起作用。
这是我的代码;
NSString *commentText = commentTextView.text;
NSUInteger textLength = [commentText length];
NSString *atSign = @"@";
NSMutableArray *commentArray = [[NSMutableArray alloc] init];
[commentArray addObject:commentText];
for (int arrayCounter=1; arrayCounter<=textLength; arrayCounter++)
{
NSRange isRange = [commentText rangeOfString:atSign options:NSCaseInsensitiveSearch];
if(isRange.location != NSNotFound)
{
commentText = [commentText stringByReplacingOccurrencesOfString:commentText withString:atSign];
[_mentionsearch filtrele:_mentionText];
id<textSearchProtocol> delegate;
[delegate closeList:[[self.searchResult valueForKey:@"user_name"] objectAtIndex:indexPath.row]];
}
}
好的,现在我可以在文本中找到“@”符号,我可以匹配它。但这是问题的根源,我不能用“@”符号替换任何字符串。这是代码的最后一部分;
-(void)closeList
{
NSArray *arrayWithSign = [commentTextView.text componentsSeparatedByString:@" "];
NSMutableArray *arrayCopy = [arrayWithSign mutableCopy];
[arrayCopy replaceObjectAtIndex:isRange.location withObject:[NSString stringWithFormat:@"@%@",username]];
}
当IM日志isRange.location值,它返回正确的。但是当我尝试运行时,我的应用程序崩溃了。所以,我无法替代[NSString stringWithFormat:@“@%@”,用户名]参数。我怎么解决这个问题?
它是如何崩溃?你必须包含崩溃日志。 – jrturton
你为什么试图使用从字符串搜索中取出的范围作为数组的索引? – Wain
没有日志缺陷。只有我有,(lldb)0x6ae8952:jae 0x6ae8962; __pthread_kill + 26. –