1

I have a NSArray of Contact objects, we can call it contacts. Contact is a superclass, FacebookGroup and Individual are subclasses of Contact. FacebookGroup has a property called individuals which is a set of Individual objects.

I also have a NSArray of NSString objects, we can call it userIDs.

What I want to do is create a new NSArray from the existing contacts array that match the userIDs in userIDs.

So if contacts has 3 Contact objects with userID 1,2 and 3. And my userIDs has a NSString object 3. Then I want the resulting array to contain Contact which equals userID 3.

Contact.h

Contact : NSObject

FacebookGroup.h

FacebookGroup : Contact

@property (nonatomic, strong) NSSet *individuals;

Individual.h

Individual : Contact

@property (nonatomic, strong) NSString *userID;

3 Answers 3

4
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId = %@", myContact.userId];
NSArray *filteredArray = [contacts filteredArrayUsingPredicate:predicate];
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I forgot to add some information. The Contact object is actually a superclass which doesn't have the userID property. The subclasses FacebookGroup and Individual have the userID property. See my updated question.
0

Is this what you are looking for?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID IN %@", userIDs];
NSArray *filtered = [contacts filteredArrayUsingPredicate:predicate];

3 Comments

Sorry I forgot to add some information. The Contact object is actually a superclass which doesn't have the userID property. The subclasses FacebookGroup and Individual have the userID property. See my updated question.
@PeterWarbo: OK, but I think that the code should work for that case as well, did you try it?
Sorry, I keep forgetting information. FacebookGroup does not have a property userID instead it has a property for a NSSet of Individual objects. I updated my question.
-1

i'm expecting you want like this once see this one,

 NSMutableArray *names = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", @"four", nil];
    NSMutableArray *ids = [NSMutableArray arrayWithObjects:@"1", @"2", @"2", @"3", nil];
    NSMutableArray *array=[[NSMutableArray alloc]init];
    for(int i=0;i<[ids count];i++){
       if([[ids objectAtIndex:i] isEqualToString:@"2"])
           [array addObject:[names objectAtIndex:i]];
    }
    NSLog(@"%@",array);

O/P:-

(
    two,
    three
)

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.