如何获取 NSMutableArray 中对象(多次出现)的索引?

发布于 2024-12-13 22:07:11 字数 287 浏览 0 评论 0原文

大家好:)

我有一个简单的问题
- 对于以下 NSMutableArray:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2 ",@"1",@"2",@"1"];

如何获取一个对象的所有索引;据说我想获取 1 的索引?
所以应该说 1 存在于索引 0、2 和 4 处。

非常感谢任何形式的帮助:)
谢谢。

Hello folks :)

I have a quick question
- For the following NSMutableArray:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1"];

How can we get all the indexes for an object; supposedly I want to grab the index of 1?
So it should say 1 exists at index 0, 2 and 4.

Any kind of help is really appreciated :)
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夏有森光若流苏 2024-12-20 22:07:11
- (NSIndexSet *)indexesMatchingObject:(id)anObject inArray:(NSArray *)anArray
{
    NSIndexSet *indexSet = [anArray indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqual:anObject];
    }];

    return indexSet;
}

一样使用它

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1",nil];
NSIndexSet *matchesIndexSet = [self indexesMatchingObject:@"1" inArray:array];
NSLog(@"%@",matchesIndexSet);

像退货

[索引数量:3(3个范围),索引:(0 2 4)]

- (NSIndexSet *)indexesMatchingObject:(id)anObject inArray:(NSArray *)anArray
{
    NSIndexSet *indexSet = [anArray indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqual:anObject];
    }];

    return indexSet;
}

use it like

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1",nil];
NSIndexSet *matchesIndexSet = [self indexesMatchingObject:@"1" inArray:array];
NSLog(@"%@",matchesIndexSet);

returns

[number of indexes: 3 (in 3 ranges), indexes: (0 2 4)]

伴随着你 2024-12-20 22:07:11

你尝试过这个吗:

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

这会给出:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1", nil];

NSIndexSet * index = [array indexesOfObjectsPassingTest:^BOOL (id obj, NSUInteger idx, BOOL *stop){
    NSString* aString = obj;
    return [aString isEqualToString:@"1"];
}];

最重要的是,你可以创建一个像这样的 NSArray 类别:

.m 文件

@implementation NSArray(Upgraded)

- (NSIndexSet*) indexesMatchingObject:(id)objectToSearch
{
    NSIndexSet *index = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqual:objectToSearch]);
    }];

    return index;
}

@end

可以像这样使用:

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1", nil];
NSIndexSet* index = [array indexesMatchingObject:@"1"];

Did you try this :

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

That would give :

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1", nil];

NSIndexSet * index = [array indexesOfObjectsPassingTest:^BOOL (id obj, NSUInteger idx, BOOL *stop){
    NSString* aString = obj;
    return [aString isEqualToString:@"1"];
}];

Best of all, you could create a NSArray category like this one :

.m file

@implementation NSArray(Upgraded)

- (NSIndexSet*) indexesMatchingObject:(id)objectToSearch
{
    NSIndexSet *index = [self indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
        return [obj isEqual:objectToSearch]);
    }];

    return index;
}

@end

That could be used like this :

NSMutableArray *array = [[NSMutableArray alloc] initWithObjects: @"1", @"2",@"1",@"2",@"1", nil];
NSIndexSet* index = [array indexesMatchingObject:@"1"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文