从聊天室过滤掉不恰当的单词

时间:2014-04-25 22:29:09

标签: ios objective-c nsstring chatroom

基本上,下面我发布了一个在聊天室发帖聊天的功能(当然,调用其他功能)。这非常有效。但是,我的应用程序被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];




    }

1 个答案:

答案 0 :(得分:2)

为什么你不要像在

那样循环运行
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];
}