Finding a specific element in an NSArrayController
Asked Answered
R

2

6

I've got an NSArrayController that contains a few elements. These elements has got a few attributes like 'name', 'interformation' etc.

What i want is simply to find an element in the NSArrayController which has the name attribute set to, lets say, 'Mads'.

Since efficiency isn't an great issue here i would just do a linear search by iterating over all the elements in the NSArrayController while checking if the 'name' attribute is 'Mads'.

But I can't seem to get an NSIterator from the NSArrayController, so I'm wondering if there's another way to do this?

Any help is appreciated

Raffin answered 8/8, 2009 at 11:44 Comment(0)
S
5

How about using the content?

ie

// ac is an NSArrayController*
for (MyObject *mob in ac.content) {
    if ([mob.name isEqualToString:@"something"]) {
        // found
        break;
    }
}
Schroth answered 8/8, 2009 at 12:0 Comment(0)
S
10

Get the arrangedObjects, which is an array, and either iterate on that or use filteredArrayUsingPredicate:.

That's assuming that it wouldn't be more appropriate to set the filterPredicate of the array controller. If you do go that way, then arrangedObjects will contain only the matching objects.

Shemikashemite answered 8/8, 2009 at 12:11 Comment(1)
Can't accept two answers to one question apparently :/ but this answer is equally.Raffin
S
5

How about using the content?

ie

// ac is an NSArrayController*
for (MyObject *mob in ac.content) {
    if ([mob.name isEqualToString:@"something"]) {
        // found
        break;
    }
}
Schroth answered 8/8, 2009 at 12:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.