ios - NSMutableArray removeObject 没有调用我的自定义 isEqual

标签 ios nsmutablearray nsobject

我有一个类,我用它来传递带有文本标签的选择器:

@interface TableLink : NSObject
@property (nonatomic, strong) NSString* name;
@property (nonatomic) NSInteger identifier;
@property (nonatomic) SEL selector;
@end
@implementation TableLink
- (id) initWithName: (NSString*) name identifier: (NSInteger) identifier selector: (SEL) selector
{
    if (self = [super init])
    {
        self.name = name;
        self.identifier = identifier;
        self.selector = selector;
    }

    return self;
}

- (BOOL) isEqual: (id) anObject
{
    if (self == anObject)
    {
        return YES;
    }
    else if ([anObject isKindOfClass: [self class]])
    {
        return [self.name isEqualToString: [anObject name]];
    }
    else if ([anObject isKindOfClass: [NSString class]])
    {
        return [self.name isEqualToString: anObject];
    }
    else
    {
        return NO;
    }
}

- (NSUInteger) hash
{
    return [self.name hash];
}

@end

我像这样设置了这些对象的可变数组:

+ (NSMutableArray*) buttonNames
{
    NSArray* names = [self determineActiveNames];
    NSMutableArray* buttons = [NSMutableArray arrayWithCapacity: names.count];
    for (int i = 0; i < names.count; i++)
    {
        NSString* s = [NSString stringWithFormat: @"show%@", names[i]];
        s = [s stringByReplacingOccurrencesOfString: @" " withString: @""];
        SEL sel = NSSelectorFromString(s);
        TableLink* tl = [[TableLink alloc] initWithName: names[i] identifier: i selector: sel];
        [buttons addObject: tl];
    }

    return buttons;
}

我是这样称呼移除的:

- (void) hideEntry: (NSString*) entry
{
    [self.buttons removeObject: entry];
    [self.tableView reloadData];
}

但是当我尝试使用 [self.buttons removeObject: someNSString]; 删除一个时,它不会从我的数组中删除任何内容。我在 isEqual: 调用中放置了一个断点,但它没有被调用。我是不是遗漏了一些东西来让它使用我的相等性测试,或者 [NSMutableArray removeObject:] 没有使用 isEqual:(文档说它确实如此)?

当我用以下代码替换对 removeObject: 的调用时,它会按预期删除我的条目。显然,它不会调用我的 isEqual:hash,但它会得到我想要的结果。我仍然想知道为什么它不起作用,以防将来我需要覆盖 isEqual。

for (NSUInteger i = 0; i < self.buttons.count; i++)
{
    if ([[self.buttons[i] name] isEqualToString: entry])
    {
        [self.buttons removeObjectAtIndex: i];
        break;
    }
}

最佳答案

您认为可变数组中有一个 TableLink 对象,您将其名称作为 NSString* 传递,并且您认为 removeObject 将调用 [someTableLink isEqual: someString] 来找到要删除的 TableLink 对象。但它也有可能调用 [someString isEqual: someTableLink],而不是调用 NSString isEqual。

你的所作所为很危险,会给你带来麻烦。如果您确定按钮名称是唯一的,请将链接放入以名称作为键的 NSDictionary 中。

关于ios - NSMutableArray removeObject 没有调用我的自定义 isEqual,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22106424/

相关文章:

Swift OSX - 设置值时无法识别的选择器发送到 NSObject 子类上的实例

swift - 为什么对象只有从 NSObject 继承时才会变成 NSZombie?

objective-c - 基于 NSObject 的类的 iOS JSON 序列化

ios - 当我们已经有一个单元格时尝试设置滑动以删除单元格....这看起来不太好

ios - 核心功能的不同用户界面

ios - 如何将 View 添加为 UITableViewCell 的 subview 但位于所有其他 View 之上

iphone - NSMutableArray UIButtons Action ?

ios - CCSprite 不会将自己添加到 NSArray

ios - AudioKit - 在启动时删除 AKNodeOutputPlot 中心线

ios - iOS-NSMutableArray是不可变的