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

6
  • Remove duplicate objects from an NSMutableArray and check micpringle answer. Commented Feb 21, 2013 at 6: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? Commented Feb 21, 2013 at 6: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? Commented Feb 21, 2013 at 6:48
  • Please post your both NSArray with set objects. Commented Feb 21, 2013 at 6:48
  • @Bhargavi : Its just 2 NSArray with the Set Objects Commented Feb 21, 2013 at 6:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.