NSMutableSet 不保持元素唯一
我有一个名为“Site”的自定义类:
#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
我有一个名为 allSites 的 NSMutableSet,我通过 unionSet 方法向其中添加其他站点集。此操作有效,并且站点集全部添加到 allSites 集中。但重复的站点不会被删除。我怀疑这与我在 Site 的 isEqual 或 hashcode 实现中的错误有关,我理解 NSMutableSet 使用它来确保唯一性。
任何见解将不胜感激。
I have a custom class called 'Site':
#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
I've got an NSMutableSet called allSites which I'm adding other sets of sites to via the unionSet method. This works and the sets of sites are all added to the allSites set. But duplicate sites are not removed. I suspect that this has something to do with a mistake on my part in the isEqual or hashcode implementation of Site, which I understand NSMutableSet uses to ensure uniqueness.
Any insight would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改 isEqual 方法:
Change the
isEqual
method:您的
Site
类的超类是什么?对超类的isEqual:
方法的调用看起来有点可疑,特别是如果您的类是NSObject
的直接后代。在这种情况下,[super isEquals: other]
本质上可以归结为self == other
,这显然不是您想要的。例如,在 可可的编码指南:这只是一个猜测,不过……
What is the superclass of your
Site
class? The call to the superclass'isEqual:
method looks a little bit suspicous, in particular, if your class is a direct descendant ofNSObject
. In that case,[super isEquals: other]
essentially boils down toself == other
, which is clearly not what you want. This is discussed, for example, in the coding guidelines for cocoa:This is just a guess, though...
超类是 NSObject。我遵循苹果推荐的 isEqual 实现:
http ://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/ObjectComparison.html
我不太熟悉 NSObject isEqual 实现。
@phix23。是的,这有效。 @Dirk,感谢您的解释。非常感谢大家,你们在调试器中为我节省了很多时间。
The superclass is NSObject. I was following apple's recommended isEqual implementation from:
http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/ObjectComparison.html
I wasn't too familiar with the NSObject isEqual implementation.
@phix23. Yep, this works. @Dirk, thanks for the explanation. Thanks a ton guys, you just saved me a bunch of time in the debugger.