所以我想检查我的NSArray中的NSString是否是我的字符串名为imageName的子字符串。
所以我们这样说:
所以我想要消除:picture5of-图像名称的一部分,并用NSArray中的NSString替换它。
这是我尝试这样做但它从未进入if语句。并且我的数组也不是零。这是代码:
for (NSString *string in superheroArray) {
if ([string rangeOfString:imageName].location != NSNotFound) {
//Ok so some string in superheroArray is equal to the file name of the image
imageName = [imageName stringByReplacingOccurrencesOfString:@"" withString:string
options:NSCaseInsensitiveSearch range:NSMakeRange(0, string.length)];
}
}
Edit1 :这仍然不起作用
for (NSString *string in superheroArray) {
if ([imageName rangeOfString:string options:NSCaseInsensitiveSearch].location != NSNotFound) {
//Ok so some string in superheroArray is equal to the file name of the image
imageName = string;
//HOW ABOUT THAT FOR EFFICIENCY :P
}
}
答案 0 :(得分:5)
[imageName rangeOfString:string options: NSCaseInsensitiveSearch]
答案 1 :(得分:2)
我不明白为什么它不能在你的代码中工作,也许从NSRage测试中拆分NSString的东西。
但这项工作在这里:
NSArray *ar = [NSArray arrayWithObjects:@"Batman", @"Maurice", nil];
__block NSString *imageName = @"picture5of-batman.png";
__block NSUInteger theIndex = -1;
[ar enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSRange r = [imageName rangeOfString: obj
options: NSCaseInsensitiveSearch];
if (r.location != NSNotFound)
{
theIndex = idx;
NSString *str = [imageName pathExtension];
imageName = [(NSString *)obj stringByAppendingPathExtension:str];
// you found it, so you can stop now
*stop = YES;
}
}];
if (theIndex != -1)
{
NSLog(@"The index is : %d and new imageName == %@", theIndex, imageName);
}
这是NSLog声明:
2011-12-10 23:04:28.967 testSwitch1 [2493:207]索引是:0和new imageName == Batman.png