我可以使用类别来添加类方法吗? [英] Can I use Categories to add class methods?

查看:224
本文介绍了我可以使用类别来添加类方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一些类方法到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.

这篇关于我可以使用类别来添加类方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆