求兩個自定義物件構成的陣列的差集
#import "ViewController.h"
@interface Person : NSObject
@property(nonatomic,strong)NSString* name;
-(instancetype)initWithName:(NSString*)name;
@end
@implementation Person
-(instancetype)initWithName:(NSString*)name{
self = [super init];
self.name = name;
return self;
}
@end
@implementation
- (void)viewDidLoad
{
[superviewDidLoad];
NSArray* arr1 = @[
[[Person alloc] initWithName:@"tom"]
,[[Person alloc] initWithName:@"jerry"]
,[[Person alloc] initWithName:@"david"]
]
NSArray* arr2 = @[
[[Person alloc] initWithName:@"tom"]
,[[Person alloc] initWithName:@"marry"]
,[[Person alloc] initWithName:@"gorge"]
];
//關鍵,name是自定義物件的屬性名
NSPredicate *thePredicate = [NSPredicate
//求差集:arr1 - arr2
NSArray* arr1SubtractArr2 = [arr1 filteredArrayUsingPredicate:thePredicate];
for(Person* person in arr1SubtractArr2){
NSLog(@"%@",person.name);
}
}
@end