Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

objective c - Filter NSArray of custom objects

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId = %@", myContact.userId];
NSArray *filteredArray = [contacts filteredArrayUsingPredicate:predicate];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...