#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 ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
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 predicateWithFormat:@"NOT (SELF.name in %@.name)", arr2];
//求差集:arr1 - arr2
NSArray* arr1SubtractArr2 = [arr1 filteredArrayUsingPredicate:thePredicate];
for(Person* person in arr1SubtractArr2){
NSLog(@"%@",person.name);
}
}
@end