2
我想使用NSMutableSet創建一組對象。該對象是一首歌曲,每個標籤都有一個名稱和一個作者。NSMutableSet removeObject無法刪除對象
代碼:
#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:
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];
}
}
}
但奇怪的happend,music3仍處於NSMutableSet.But變化的NSMutableArray,該music3可以刪除。 NSMutableArray的removeObject調用對象的isEqual方法。我覺得removeObject.Just句子的解釋:
Removes a given object from the set.
這不是解釋它是如何works.How刪除對象像這樣的NSMutableSet的的removeObject調用哪個方法?
非常感謝! – user1971788