【发布时间】:2014-01-14 23:35:42
【问题描述】:
我已经扩展了使用It 设置的黑名单单词,只是为了发现一些单元测试现在失败了。
我没有注意到注释中的 I 现在也被排除在外,因为它匹配 It,这不是我想要的。
notes = @"I was out with Jenny for dinner. It was raining all night.";
NSString * const BLACKLISTEDWORDS = @"in,it,It";
NSArray *words = [notes componentsSeparatedByString:@" "];
for (NSString *word in words) {
if([BLACKLISTEDWORDS rangeOfString:word].location == NSNotFound]) {
}
}
有没有更好的方法来创建黑名单方法?
解决方案: 哑光解决方案工作正常。将其捕获为该解决方案的代码:
NSSet *blackList = [NSSet setWithArray:[BLACKLISTEDWORDS componentsSeparatedByString:@","]];
for (NSString *word in words) {
if (![blackList containsObject:word]) {
}
}
【问题讨论】: