Ios 从另一个同类对象数组中减去一个复杂对象数组

Ios 从另一个同类对象数组中减去一个复杂对象数组,ios,objective-c,ipad,nsarray,Ios,Objective C,Ipad,Nsarray,我知道从NSArray中减去一个NSArray,如果它是一个基本对象 但我有一个像这样的物体 @interface Set : NSObject @property (nonatomic, strong) NSString *ItemId; @property (nonatomic, strong) NSString *time; @property (nonatomic, strong) NSString *Category_id; @property (nonatomic, strong)

我知道从NSArray中减去一个NSArray,如果它是一个基本对象

但我有一个像这样的物体

@interface Set : NSObject
@property (nonatomic, strong) NSString *ItemId;
@property (nonatomic, strong) NSString *time;
@property (nonatomic, strong) NSString *Category_id;
@property (nonatomic, strong) NSString *List_id;
@property (nonatomic, strong) NSString *name;
@end
如何从另一个具有相同集合对象的数组中删除具有该集合对象的数组? 这可以通过迭代来完成,我知道。还有其他方法吗

编辑:为了清晰起见

我有数组A和5个集合对象,在数组B中有4个集合对象 数组A和数组B包含3个具有公共值的集合对象。[注意:内存可能不同。]公共

我只需要一个数组C=Array A-Array B,它在生成的数组C中有2个对象


谢谢:)

您需要在
集合
类中实现
-(NSUInteger)散列
-(BOOL)isEqual:(id)对象
方法

例如:-

- (NSUInteger)hash {
   return [self.ItemId hash];
}

- (BOOL)isEqual:(id)object
{
    return ([object isKindOfClass:[self class]] &&
            [[object ItemId] isEqual:_ItemId])

}
之后,请尝试以下操作:

NSMutableSet *set1 = [NSMutableSet setWithArray:array1];
NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets

NSArray *commonItems = [set1 allObjects];

[mutableArray1 removeObjectsInArray:commonItems];//mutableArray1 is the mutable copy of array1

mutableArray1
删除公共对象后,所有对象的顺序将与前面相同。

您需要在
Set
类中实现
-(NSUInteger)hash
-(BOOL)isEqual:(id)object
方法

例如:-

- (NSUInteger)hash {
   return [self.ItemId hash];
}

- (BOOL)isEqual:(id)object
{
    return ([object isKindOfClass:[self class]] &&
            [[object ItemId] isEqual:_ItemId])

}
之后,请尝试以下操作:

NSMutableSet *set1 = [NSMutableSet setWithArray:array1];
NSMutableSet *set2 = [NSMutableSet setWithArray:array2];
[set1 intersectSet:set2]; //this will give you only the obejcts that are in both sets

NSArray *commonItems = [set1 allObjects];

[mutableArray1 removeObjectsInArray:commonItems];//mutableArray1 is the mutable copy of array1

mutableArray1
删除常用对象后,所有对象的顺序将与前面相同。

通过使用
NSSet
NSPredicate
我们可以满足您的要求

Assessors *ass1 = [[Assessors alloc] init];
ass1.AssessorID = @"3";

Assessors *ass2 = [[Assessors alloc] init];
ass2.AssessorID = @"2";

Assessors *ass3 = [[Assessors alloc] init];
ass3.AssessorID = @"1";

Assessors *ass4 = [[Assessors alloc] init];
ass4.AssessorID = @"2";

NSSet *nsset1 = [NSSet setWithObjects:ass1, ass2,  nil];
NSSet *nsset2 = [NSSet setWithObjects:ass3, ass4, nil];

// retrieve the IDs of the objects in nsset2
NSSet *nsset2_ids = [nsset2 valueForKey:@"AssessorID"];

// only keep the objects of nsset1 whose 'id' are not in nsset2_ids
NSSet *nsset1_minus_nsset2 = [nsset1 filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"NOT AssessorID IN %@",nsset2_ids]];

for(Assessors *a in nsset1_minus_nsset2)
    NSLog(@"Unique ID : %@",a.AssessorID);
这里Assessors是我的NSObject类(在您的案例中设置),AssessorID是该类的一个属性


希望这能有所帮助。

通过使用
NSSet
NSPredicate
我们可以满足您的要求

Assessors *ass1 = [[Assessors alloc] init];
ass1.AssessorID = @"3";

Assessors *ass2 = [[Assessors alloc] init];
ass2.AssessorID = @"2";

Assessors *ass3 = [[Assessors alloc] init];
ass3.AssessorID = @"1";

Assessors *ass4 = [[Assessors alloc] init];
ass4.AssessorID = @"2";

NSSet *nsset1 = [NSSet setWithObjects:ass1, ass2,  nil];
NSSet *nsset2 = [NSSet setWithObjects:ass3, ass4, nil];

// retrieve the IDs of the objects in nsset2
NSSet *nsset2_ids = [nsset2 valueForKey:@"AssessorID"];

// only keep the objects of nsset1 whose 'id' are not in nsset2_ids
NSSet *nsset1_minus_nsset2 = [nsset1 filteredSetUsingPredicate:[NSPredicate predicateWithFormat:@"NOT AssessorID IN %@",nsset2_ids]];

for(Assessors *a in nsset1_minus_nsset2)
    NSLog(@"Unique ID : %@",a.AssessorID);
这里Assessors是我的NSObject类(在您的案例中设置),AssessorID是该类的一个属性


希望这能有所帮助。

并检查micpringle的答案。检查它说它是关于通过在循环中比较和迭代来删除重复项…我知道这种方法。还有其他方法吗?让我了解。。。您有两个数组arr1和arr2,它们的对象都是
Set
。您要查找
resultar=arr1-arr2
。是吗?请用集合对象发布你的两个NSArray。@Bhargavi:只有2个NSArray和集合对象,并检查micpringle答案。检查它说它是关于通过循环比较和itrating来删除重复项…我知道这种方式。还有其他方法吗?让我理解。。。您有两个数组arr1和arr2,它们的对象都是
Set
。您要查找
resultar=arr1-arr2
。是吗?请用集合对象发布你的两个NSArray。@Bhargavi:只有2个NSArray和集合对象