1

I want to get some objects from array contains objects where one of objects property satisfy the condition

Like in c#

example :

NSMutablearray * coursesinfo ;

this array contain more than 30 courses

course is object on of its property is finalgrade

i want to get all courses where final grade < 100

can i do this in objective -c like c# ? and how?

4
  • Just use the for each loop and check the condition? What is the problem here? Commented May 27, 2012 at 1:44
  • 1
    i asked if there another solution like where statement in C# Commented May 27, 2012 at 1:50
  • Read the spec. There are several versions of indexOfObjectPassingTest, along with several different sorting options. Commented May 27, 2012 at 3:33
  • 1
    Why some many downvotes? That's a good question, objective-c looks quite lame after using c# LINQ and lambda expressions. Commented Jul 8, 2014 at 5:43

2 Answers 2

4

the where statement is like using a predicate in cocoa/cocoa touch. here is an example, where I have an array of image file names from a directory and I am looking for the base file name. The indexesOfObjectsWithOptions: method returns a set of Indices that pass the specific test. NSEnumerationConcurrent utilizes a concurrent queue to take advantage of multiple cores, if present.

NSIndexSet *indexSet=[allImageURLs indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    BOOL match=NO;
    NSRange twoXRange=[((NSURL *)obj).absoluteString rangeOfString:@"@2x"];
    NSRange iPhoneRange=[((NSURL *)obj).absoluteString rangeOfString:@"~ipad"];
    if (twoXRange.location==NSNotFound && iPhoneRange.location==NSNotFound) {
        match=YES;
    }
    return  match;
}];

self.imageURLs=[allImageURLs objectsAtIndexes: indexSet];

for your particular case I would do the following:

NSIndexSet *theSet=[coursesinfo indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    BOOL match=NO;

    if( obj.finalGrade<100 ){
       match=YES;
    }
    return match;
}];

NSArray *courses=[coursesinfo objectsAtIndexes: theSet];

Good luck!

t

Sign up to request clarification or add additional context in comments.

Comments

0

The closest is use - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate function of NSArray.

Link to NSPredicate documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.