我有所谓的“网站”的自定义类:的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