我正在尝试使用 NSMutableSet 创建一组对象。对象是一首歌曲,每个标签都有一个名字和一个作者。
代码:
#import "Song.h"
@implementation Song
@synthesize name,author;
-(Song *)initWithName:(NSString *)n andAuth:(NSString *)a {
self = [super init];
if (self) {
name = n;
author = a;
}
return self;
}
-(void)print {
NSLog(@"song:%@; author:%@;", name,author);
}
-(BOOL)isEqual:(id)obj {
//NSLog(@"..isEqual");
if([[obj name] isEqualToString:name]
&& [[obj author] isEqualToString:author]) {
return YES;
}
return NO;
}
-(BOOL)isEqualTo:(id)obj {
NSLog(@"..isEqualTo");
if([[obj name] isEqualToString:name]
&& [[obj author] isEqualToString:author]) {
return YES;
}
return NO;
}
@end
然后把这个对象放入NSMutableSet:</p>
int main(int argv, char *argc[]) {
@autoreleasepool {
Song *song1 = [[Song alloc] initWithName:@"music1" andAuth:@"a1"];
Song *song2 = [[Song alloc] initWithName:@"music2" andAuth:@"a2"];
Song *song3 = [[Song alloc] initWithName:@"music3" andAuth:@"a3"];
Song *needToRemove = [[Song alloc] initWithName:@"music3" andAuth:@"a3"];
NSMutableSet *ns = [NSMutableSet setWithObjects:song1, song2, song3, nil];
[ns removeObject:needToRemove];
for (Song *so in ns) {
[so print];
}
}
}
但是奇怪的事情发生了,music3还在NSMutableSet中。但是换成NSMutableArray,music3就可以删除了。NSMutableArray的removeObject调用对象的isEqual方法。我找到了removeObject的解释。就一句话:</p>
Removes a given object from the set.
不解释它是如何工作的。如何像这样删除对象?NSMutableSet的removeObject调用哪个方法?