I have an NSMutableArray called myObjectArray which contains and array of NSObjects called myObject. myObject has two fields (elements?) which are NSString‘s. like this:
@interface myObject : NSObject {
NSString * string1;
NSString * string2;
}
I have an NSMutableArray which contains about 50 of these objects, all with different string1’s and string2’s. then I have and independent NSString variable, called otherString;
Is there a fast way to access the myObject from myObjectArray whose string1 matches otherString?
i should say, this is what i have, but i wonder if there is a faster way:
-(void) matchString: {
NSString * testString = otherString;
for(int i=0; i<[myObjectArray count];i++){
myObject * tempobject = [myObjectArray objectAtIndex:i];
NSString * tempString = tempobject.string1;
if ([testString isEqualToString:tempString]) {
// do whatever
}
}
}
There are a few ways you can do this,
Using Predicates
Now
filteredArrayhas all themyObjectinstances that have theirstring1matchingotherString.Using
indexOfObjectPassingTest:If there is an object that satisfies the condition,
indexwill point you to its index. Otherwise it will have the valueNSNotFound.You can also look at
indexesOfObjectsPassingTest:if you want all the objects satisfying the condition.