[英]Filter NSArray of custom objects
我有一個NSArray
的Contact
對象,我們可以稱其為contacts
。 Contact是超類, FacebookGroup
和Individual
是Contact
子類。 FacebookGroup
具有稱為individuals
的屬性,它是一組Individual
對象。
我也有一個NSArray
的NSString
對象,我們可以把它叫做userIDs
。
我想做的是從現有的contacts
數組中創建一個與userIDs中的userIDs
相匹配的新NSArray
。
因此,如果contacts
有3個Contact
對象,其userID
userIDs
分別為1,2和3。我的userID
具有3個NSString
對象,那么我希望結果數組包含等於userID
3的Contact
。
聯絡人
Contact : NSObject
FacebookGroup.h
FacebookGroup : Contact
@property (nonatomic, strong) NSSet *individuals;
個人.h
Individual : Contact
@property (nonatomic, strong) NSString *userID;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userId = %@", myContact.userId];
NSArray *filteredArray = [contacts filteredArrayUsingPredicate:predicate];
這是你想要的?
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userID IN %@", userIDs];
NSArray *filtered = [contacts filteredArrayUsingPredicate:predicate];
我希望您一旦看到這個就想要這樣,
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
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.