0

I know that subtracting an NSArray from NSArray if it is a single basic object found here

But what i have is an object like this

@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

How can i delete an array having the set object from another array with the same? It can be done by iterations that i knw.Is there some other way ?

EDIT: For clarity

I have Array A with 5 Set objects and I have 4 Set Objects in Array B array A and Array B contain 3 set objects with common values..[Note : memory may be different] common

All I need is an Array C =Array A - Array B that has 2 objects in resulting array C

Thank You :)

Community
  • 1
  • 1
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
  • [Remove duplicate objects from an NSMutableArray](http://stackoverflow.com/q/6518714/1603234) and check *micpringle* answer. – Hemang Feb 21 '13 at 06:33
  • Checked it says that it is about removing duplicate by comparing and itrating in a loop...I knew that way.Is there some other method? – Lithu T.V Feb 21 '13 at 06:40
  • let me understand... You have 2 arrays arr1 and arr2, both having objects of `Set`. You want to find `resultArr=arr1-arr2`. is it? – Anoop Vaidya Feb 21 '13 at 06:48
  • Please post your both NSArray with set objects. – βhargavḯ Feb 21 '13 at 06:48
  • @Bhargavi : Its just 2 NSArray with the Set Objects – Lithu T.V Feb 21 '13 at 06:51
  • @LithuT.V, Check my answer. It does the same thing. But note that under the hood it will be still enumerating objects. You cant avoid that. – iDev Feb 21 '13 at 06:59

2 Answers2

3

You need to implement the - (NSUInteger)hash and - (BOOL)isEqual:(id)object method in Set class.

For eg:-

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

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

}

After that try this:

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 will have all objects in the same order as earlier after removing common objects.

iDev
  • 23,310
  • 7
  • 60
  • 85
1

By using NSSet and NSPredicate we can meet your requirement.

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);

Here Assessors is my NSObject Class (Set in your case) and AssessorID is one property of that class.

Hope this can help.

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59