我有一个带有公共高分列表的游戏,我允许图层输入他们的名称(或任何最多 12 个字符的名称)。我正在尝试创建几个函数来从坏词列表中过滤掉坏词
我有一个文本文件。我有两种方法:
读取文本文件之一:
-(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);
}
}
还有一个用来检查一个单词(NSString*)
对照我读到的清单:
-(NSString *) removeBadWords :(NSString *) string {
// If I hard code this line below, it works....
// *****************************************************************************
//badwords =[[NSMutableArray alloc] initWithObjects:@"shet",@"shat",@"shut",nil];
// *****************************************************************************
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;
}
我遇到的问题是,当我将单词硬编码到数组中(参见上面的注释)时,它就像一个魅力。但是当我使用通过第一种方法读入的数组时,它不起作用 -stringByReplacingOccurrencesOfString:words
似乎没有效果。我已经追踪到日志,这样我就可以看到这些单词是否通过了,它们是……那一行似乎看不到这些单词,除非我硬核进入数组。
有什么建议么?