基本上,下面我将发布一个在聊天室中发布聊天的功能(当然是调用其他功能)。这很完美。但是,由于我的聊天室中可能出现不适当的内容,因此我的应用程序被Apple拒绝。因此,我决定通过以下方法过滤掉不适当的内容:制作不适当的单词数组,使用for循环遍历该数组,并在用户发布的消息(在可变消息下方)中搜索不适当的单词,然后将该单词替换为*取决于单词的长度。下面是我的代码,但是我缺少一些东西。请告诉我要添加的代码,或执行此操作的更好方法。这是我的代码(注意:BLACKLIST是我的不当用词的阵列):
- (void)displayChatMessage:(NSString*)message fromUser:(NSString*)userName {
int i=0;
int goodJobRunCount = 0;
for (i=0; i<[blackList count]; i++)
{
NSString *car = [blackList objectAtIndex:i];
NSRange searchResult = [message rangeOfString:car];
if (searchResult.location == NSNotFound) {
goodJobRunCount=goodJobRunCount+1;
if (goodJobRunCount >= i){
message2= [NSString stringWithFormat:message];
}
} else{
NSUInteger length= searchResult.length;
int runLength = 0;
message2 = [NSString stringWithFormat:@""];
while (runLength < length){
NSString *string2 = [message2 stringByAppendingFormat:
@"*"];
message2 = [NSString stringWithFormat:string2];
runLength = runLength +1;
}
}
}
[chat appendTextAfterLinebreak:[NSString stringWithFormat:@"%@: %@", userName, message]];
[chat scrollToBottom:chat];
}
最佳答案
你为什么不像这样循环运行
for(int i = 0 ;i < blacklist.count;i++)
{
NSMutableString* stars = [NSMutableString alloc]init];
NSRange searchResult = [message rangeOfString:(NSString*)[blackList objectAtIndex:
if (searchResult.location == NSNotFound)
continue;//break or whatever is it in obj-c
for(int j = 0 ; j < (NSString*)[blackList objectAtIndex:i].length;j++)
[stars appendString:@"*"];
message = [message stringByReplacingOccurrencesOfString:(NSString*)[blackList objectAtIndex:i] withString:stars];
}