2013-02-21 77 views
0

我知道,減去的NSArray一個NSArray如果它是一個單一的基本對象found here從同類對象的另一陣列減去複雜對象的數組IOS

,但我已經是一個對象這樣

@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 

我該如何刪除一個具有相同設置對象的數組? 它可以通過迭代,我knw.Is有一些其他的方式?

編輯:爲了清楚

我有陣列的與5設置對象和我在陣列4個對象乙 數組A和數組b包含具有共同的值3個的對象集。[注意:存儲器可以是不同]共同

我所需要的是一個陣列C =陣列A - 數組b具有所得數組中2個畫線C

謝謝:)

+0

[從NSMutableArray中刪除重複的對象](http://stackoverflow.com/q/6518714/1603234)並檢查* micpringle *回答。 – Hemang 2013-02-21 06:33:26

+0

檢查它說它是通過比較和循環刪除重複...我知道這一點。是否有其他方法? – 2013-02-21 06:40:14

+0

讓我明白...你有2個數組arr1和arr2,都有'Set'對象。你想找到'resultArr = arr1-arr2'。是嗎? – 2013-02-21 06:48:43

回答

3

您需要實施- (NSUInteger)hash- (BOOL)isEqual:(id)object方法中的Set類。

對於如: -

- (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將在同一訂單中的所有對象作爲去除共對象後較早。

+0

@ LithuT.V,如果有幫助,您可以接受嗎? – iDev 2013-02-26 20:07:50

1

通過使用NSSetNSPredicate我們可以滿足您的要求。

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類(Set在你的情況)和AssessorID是該類的一個屬性。

希望這可以幫助。

相關問題