Question

Basically, below I am posting a function that posts a chat in a chatroom (calling other functions of course). This works perfectly. However, my app got rejected by Apple due to the possibility of inappropriate content appearing in my chatroom. So, I decided to filter out inappropriate content by making an array of inappropriate words, iterating through that array using a for loop and search in the user's posted message (under the variable message) for an inappropriate word, and replacing that word with the amount of * depending on the length of the words. Below is my code, but I am missing some stuff. Please tell me what code to add, or a better way to do this. Here is my code (NOTE: BLACKLIST IS MY ARRAY OF INAPPROPRIATE WORDS):

- (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];




    }
Was it helpful?

Solution

why don't you just run in loop something like

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top