Objective-c enumerateUsingBlock 问题

发布于 2024-12-10 19:49:25 字数 768 浏览 0 评论 0原文

我想提取所有具有前缀“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2024-12-17 19:49:25

您只能得到第一个,因为一旦通过 *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:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文