2
我已經通過使用NSSet/NSMutableSet看到了關於過濾器對象的一些問題/答案。這些案例使用簡單類型的對象,如NSString
或int
。以下是用於過濾NSString
對象代碼的例子:使用NSSet/NSMutableSet過濾對象?
NSSet smallArray = [[NSSet alloc] initWithObjects:@"0", @"1", @"2", nil];
NSArray bigArray = [[NSArray alloc] initWithObjects:@"0", @"1", @"4", @"5", ..., nil];
NSMutableSet *intersection = [NSMutableSet setWithArray:smallArray];
[intersection intersectSet:[NSSet setWithArray:bigArray];
NSArray *result = [NSArray arrayWithSet:intersection];
我不知道它是如何工作如果集合或數組對象是對象的C類的實例。以下面的課爲例:
@interface MyObject: NSObject {
int id;
NSString *name;
NSString *city;
...
}
我不知道如何set的intersetSet:與上述類型的對象一起工作。如果我想通過只比較id來得到相交集對象smallArray和bigArray,我懷疑我是否可以使用intersetSet:
選擇器方法?如果沒有,不知道是否應該添加任何方法到MyObject
類,以便set知道如何比較對象?
你的回答很有道理。我會先嚐試一下謝謝! – 2010-10-19 04:23:30