Objective-c enumerateUsingBlock 问题
我想提取所有具有前缀“be”的对象,但我只得到第一个对象,而不是所有来自各种索引的对象。 “array”包含各种对象,它包含“be”、“become”、“beta”、“be”、“beaver”等对象。这里有什么问题?
当我使用 localizedCaseInsensitiveCompare:
时,它仅显示两个“be”,这在“isEqualToString:
”方面是正确的,并且“array”实际上包含来自不同索引的两个“be” 。
代码如下:
NSString *string =@"be";
NSRange range = NSMakeRange(0, 24);
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange: range];
[array enumerateObjectsAtIndexes:indexSet options: NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger index, BOOL *stop)
{
//if([obj localizedCaseInsensitiveCompare:string] == NSOrderedSame)
if([obj hasPrefix:string])
{
NSLog(@"Object Found: %@ at index: %i",obj, index);
*stop=YES;
}
} ];
I want to extract all objects that has prefix "be", but I get only the first object, not all from various indexes. "array" contains various objects, and it contains objects like "be", "become", "beta", "be", "beaver", etc. What is wrong here?
When I use localizedCaseInsensitiveCompare:
, it shows only two "be" which is correct in terms of "isEqualToString:
" and "array" contains actually two "be" from different indexes.
The codes are as follow:
NSString *string =@"be";
NSRange range = NSMakeRange(0, 24);
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange: range];
[array enumerateObjectsAtIndexes:indexSet options: NSEnumerationConcurrent usingBlock:^(id obj, NSUInteger index, BOOL *stop)
{
//if([obj localizedCaseInsensitiveCompare:string] == NSOrderedSame)
if([obj hasPrefix:string])
{
NSLog(@"Object Found: %@ at index: %i",obj, index);
*stop=YES;
}
} ];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只能得到第一个,因为一旦通过
*stop = YES
行找到单个结果,您就会停止循环。删除它。您还应该在测试中使用
-indexesOfObjectsPassingTest:
,然后获取返回的索引集并将其传递给-objectsAtIndexes:
。You only get the first because you stop the loop as soon as you find a single result via the
*stop = YES
line. Remove that.You should also use
-indexesOfObjectsPassingTest:
with your test, then take the returned index set and pass it to-objectsAtIndexes:
.