ios - 覆盖 UIColor 的 isEqual : method within Category

标签 ios objective-c cocoa-touch uicolor

我正在尝试覆盖 UIColor isEqual: 方法。我在一个类别方法中这样做,但它似乎没有被调用,无论是从 NSArraycontainsObject:,还是直接调用,如下所示。

它已在类别的头文件中公开为一个方法,我还检查了该类别是否已导入到我正在处理的实现文件中。

直接调用的地方:

UIColor *col = [UIColor eightBitColorWithRed:pxl.red green:pxl.green blue:pxl.blue];
int index = -1;
for (int i = 0; i < self.colorArrayM.count; i++) {
    if ([col isEqual:((UIColor*)self.colorArrayM[i])]) {
        index = i;
        break;
    }
}

和类别方法:

-(BOOL) isEqual:(id)otherColor {
    if (otherColor == self)
        return YES;
    if (!otherColor || ![otherColor isKindOfClass:[self class]])
        return NO;
    return [self isEqualToColor:otherColor];
}

-(BOOL) isEqualToColor:(UIColor*)otherColor {
    if (self == otherColor)
        return YES;

    unsigned char r0, r1, g0, g1, b0, b1;
    [self eightBitRed:&r0 green:&g0 blue:&b0];
    [otherColor eightBitRed:&r1 green:&g1 blue:&b1];

    return r0 == r1 && g0 == g1 && b0 == b1;
}

最佳答案

简短的回答是类别无意覆盖现有方法:

Although the Objective-C language currently allows you to use a category to override methods the class inherits, or even methods declared in the class interface, you are strongly discouraged from doing so. A category is not a substitute for a subclass. There are several significant shortcomings to using a category to override methods:

  • When a category overrides an inherited method, the method in the category can, as usual, invoke the inherited implementation via a message to super. However, if a category overrides a method that exists in the category's class, there is no way to invoke the original implementation.

  • A category cannot reliably override methods declared in another category of the same class.

    This issue is of particular significance because many of the Cocoa classes are implemented using categories. A framework-defined method you try to override may itself have been implemented in a category, and so which implementation takes precedence is not defined.

  • The very presence of some category methods may cause behavior changes across all frameworks. For example, if you override the windowWillClose: delegate method in a category on NSObject, all window delegates in your program then respond using the category method; the behavior of all your instances of NSWindow may change. Categories you add on a framework class may cause mysterious changes in behavior and lead to crashes.

您需要调整原始的 isEqual: 方法以使用您自己的实现。有一个很棒的NSHipster article on swizzling这应该让你开始。

关于ios - 覆盖 UIColor 的 isEqual : method within Category,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29725669/

相关文章:

cocoa - 在 Xcode 中如何找出光标所在的代码行?

ios - 为什么窗口的 subview 最初定位不正确,但旋转后重新定位正确?

iphone - iPhone SDK 的 Visual Source Safe 等效项是什么?

ios - 使用 Realm 的 RLMArray 存储字符串数组

iphone - iOS - 删除存储在主 NSDictionary 中的所有 NSDictionaries 上包含键的所有条目

iOS - 在自定义 UITableViewCell 中为 UITextField 添加目标/操作

ios - 应用 Uiimageview 的图像比例和 CGaffinetransformation 值不起作用

iphone - Objective C 中符号 ^ 的含义

ios - 如何淡出表格选择

ios - Mail Composer 不会关闭 iOS