I have a game with a public highscore where I allow layers to enter their name (or something up to 12 characters). I am trying to create a couple of functions for filtering bad words from a list of bad words
I have a text file. I have two methods:
One to read in a text file:
-(void) getTheBadWordsAndSaveForLater {
badWordsFilePath = [[NSBundle mainBundle] pathForResource:@"badwords" ofType:@"txt"];
badWordFile = [[NSString alloc] initWithContentsOfFile:badWordsFilePath encoding:NSUTF8StringEncoding error:nil];
badwords =[[NSArray alloc] initWithContentsOfFile:badWordFile];
badwords = [badWordFile componentsSeparatedByString:@"\n"];
NSLog(@"Number Of Words Found in file: %i",[badwords count]);
for (NSString* words in badwords) {
NSLog(@"Word in Array----- %@",words);
}
}
And one, to check the word (NSString*)
, is again on the list in which I read:
-(NSString *) removeBadWords :(NSString *) string {
NSLog(@"checking: %@",string);
for (NSString* words in badwords) {
string = [string stringByReplacingOccurrencesOfString:words withString:@"-" options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
NSLog(@"Word in Array: %@",words);
}
NSLog(@"Cleaned Word Returned: %@",string);
return string;
}
The problem I am facing is that when I hardcode the words into an array (see above), it works like a charm. But when I use the array that I read in the first method, it does not work - stringByReplacingOccurrencesOfString:words
it seems to have no effect. I traced back to the magazine so that I could see if the words go, and they ... This one line does not seem to see the words, unless I'm hardcore in an array.
?