我有所謂的「網站」的自定義類:的NSMutableSet沒有保留元素獨特
#import "Site.h"
#import <MapKit/MapKit.h>
@implementation Site
@synthesize name, desc, coordinate;
+ (Site*) siteWithName:(NSString *)newName
andDescription:(NSString *)newDesc
andLatitude:(double)newLat
andLongitude:(double)newLon
{
Site* tmpSite = [[Site alloc] initWithName:newName
andDescription:newDesc
andLatitude:newLat
andLongitude:newLon];
[tmpSite autorelease];
return tmpSite;
}
- (Site*) initWithName:(NSString *)newName
andDescription:(NSString *)newDesc
andLatitude:(double)newLat
andLongitude:(double)newLon
{
self = [super init];
if(self){
self.name = newName;
self.desc = newDesc;
coordinate.latitude = newLat;
coordinate.longitude = newLon;
return self;
}
return nil;
}
- (NSString*) title
{
return self.name;
}
- (NSString*) subtitle
{
return self.desc;
}
- (BOOL)isEqual:(id)other {
if (other == self)
return YES;
if (![super isEqual:other])
return NO;
return [[self name] isEqualToString:[other name]]; // class-specific
}
- (NSUInteger)hash{
return [name hash];
}
- (void) dealloc
{
[name release];
[desc release];
[super dealloc];
}
@end
我有一個名爲的NSMutableSet其中allSites我通過unionSet方法添加其他組的網站來。這可以工作,並且這些網站集都會添加到allSites集中。但重複的網站不會被刪除。我懷疑這與我在網站的isEqual或hashcode實現中出現的錯誤有關,我知道NSMutableSet用它來確保唯一性。
任何有識之士將不勝感激。
設置斷點的isEqual中,看看它實際上是所謂的 – Felix 2011-02-07 15:29:43