我可以使用类别来添加类方法吗?

我可以使用类别来添加类方法吗?

问题描述:

我想添加一些类方法到UIColor。我实现了它们,一切编译正常,但在运行时我得到以下错误:

I want to add some class methods to UIColor. I've implemented them and everything compiles fine, but at runtime I get the following error:


由于未捕获异常终止应用程序'NSInvalidArgumentException' ,原因:'+ [UIColor colorWithHex:]:无法识别的选择器发送到类0x8d1d68'

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIColor colorWithHex:]: unrecognized selector sent to class 0x8d1d68'

这是头文件:

@interface UIColor (Hex) 
+ (UIColor*) colorWithHex: (NSUInteger) hex;
@end

这是实现:

#import "UIColor+Hex.h"


@implementation UIColor (Hex)

+ (UIColor*) colorWithHex: (NSUInteger) hex {
    CGFloat red, green, blue, alpha;

    red = ((CGFloat)((hex >> 16) & 0xFF)) / ((CGFloat)0xFF);
    green = ((CGFloat)((hex >> 8) & 0xFF)) / ((CGFloat)0xFF);
    blue = ((CGFloat)((hex >> 0) & 0xFF)) / ((CGFloat)0xFF);
    alpha = hex > 0xFFFFFF ? ((CGFloat)((hex >> 24) & 0xFF)) / ((CGFloat)0xFF) : 1;

    return [UIColor colorWithRed: red green:green blue:blue alpha:alpha];
}
@end



我找到了一些关于添加-all_load链接器标志,但这样做给出相同的结果。这是在iPhone上,如果不清楚。

I've found something about adding -all_load to the linker flags, but doing that gives the same result. This is on the iPhone, if it wasn't clear.

在将构建配置更改为释放并返回调试后,它的工作方式类似于charm。
奇怪。

Hm. After changing the build configuration to release and back to debug, it worked like a charm. Strange.