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:
我想向UIColor添加一些类方法。我已经实现了它们,所有的东西都编译得很好,但是在运行时,我得到了以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UIColor colorWithHex:]: unrecognized selector sent to class 0x8d1d68'
由于未捕获异常“NSInvalidArgumentException”而终止应用程序,原因:“+[UIColor colorWithHex:]:发送给类0x8d1d68的无法识别的选择器”
Here's the header file:
头文件:
@interface UIColor (Hex)
+ (UIColor*) colorWithHex: (NSUInteger) hex;
@end
Here's the implementation:
这是实现:
#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
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.
我已经找到了向链接器标记添加-all_load的方法,但是这样做会得到相同的结果。这是在iPhone上,如果不清楚的话。
7 个解决方案
#1
8
Yes, you can do this. You're probably not compiling the .m into your project.
是的,你可以做到。您可能没有将.m编译到您的项目中。
#2
2
You need to add the -ObjC
and -all_load
linker flags in the “Other Linker Flags” setting.
您需要在“其他链接器标志”设置中添加-ObjC和-all_load链接器标志。
#3
1
You can, but you should be very careful about adding methods (instance or class) to framework classes. If a method with the same name but different semantics exists anywhere, the effects are undefined. In particular, there could be a private system framework method with the same name, or one might be added by a future OS release, or (worst of all) it could be added by some other bundle, including input managers, colour pickers and other code injection mechanisms. This is an actual problem that does occur in real life.
可以,但是在向框架类添加方法(实例或类)时应该非常小心。如果在任何地方都存在同名但语义不同的方法,则不定义其效果。特别是,可能会有一个具有相同名称的私有系统框架方法,或者可能会在将来的OS版本中添加一个,或者(最糟糕的是)可能会添加其他一些包,包括输入管理器、颜色选择器和其他代码注入机制。这是现实生活中确实存在的问题。
There are basically two options for fixing this : 1) Don’t Do That (for example, use a standard C function instead), or 2) take steps to reduce the chance of a name conflict using a prefix, like in class names – say, inferis_colorWithHex:
.
解决这个问题基本上有两种选择:1)不要这么做(例如,使用标准的C函数),或者2)采取步骤减少使用前缀(如类名中的inferis_colorWithHex:)进行名称冲突的机会。
#4
0
You definitely can do it. I'm guessing that there is something wrong within @implementation UIColor(Hex)
你一定能做到。我猜@implementation UIColor(Hex)有问题
#5
0
Have you import'd the .h file in the class you are calling this method? Also, check if all .m files are in the target.
您是否在正在调用此方法的类中导入了.h文件?另外,检查所有.m文件是否在目标文件中。
#6
0
Hm. After changing the build configuration to release and back to debug, it worked like a charm. Strange.
嗯。在将构建配置更改为发布配置并返回到debug之后,它的工作效果非常好。奇怪。
#7
0
I know it's late but JiC it helps anyone else tearing their hair out for hours, if you are using CocoaPods in the workspace and have specified the use_frameworks directive this seems to have an effect on whether Category source code in other Frameworks gets loaded.
我知道已经很晚了,但是JiC可以帮助其他任何人在工作空间中使用CocoaPods并指定use_frameworks指令,这似乎会影响其他框架中的类别源代码是否被加载。
#1
8
Yes, you can do this. You're probably not compiling the .m into your project.
是的,你可以做到。您可能没有将.m编译到您的项目中。
#2
2
You need to add the -ObjC
and -all_load
linker flags in the “Other Linker Flags” setting.
您需要在“其他链接器标志”设置中添加-ObjC和-all_load链接器标志。
#3
1
You can, but you should be very careful about adding methods (instance or class) to framework classes. If a method with the same name but different semantics exists anywhere, the effects are undefined. In particular, there could be a private system framework method with the same name, or one might be added by a future OS release, or (worst of all) it could be added by some other bundle, including input managers, colour pickers and other code injection mechanisms. This is an actual problem that does occur in real life.
可以,但是在向框架类添加方法(实例或类)时应该非常小心。如果在任何地方都存在同名但语义不同的方法,则不定义其效果。特别是,可能会有一个具有相同名称的私有系统框架方法,或者可能会在将来的OS版本中添加一个,或者(最糟糕的是)可能会添加其他一些包,包括输入管理器、颜色选择器和其他代码注入机制。这是现实生活中确实存在的问题。
There are basically two options for fixing this : 1) Don’t Do That (for example, use a standard C function instead), or 2) take steps to reduce the chance of a name conflict using a prefix, like in class names – say, inferis_colorWithHex:
.
解决这个问题基本上有两种选择:1)不要这么做(例如,使用标准的C函数),或者2)采取步骤减少使用前缀(如类名中的inferis_colorWithHex:)进行名称冲突的机会。
#4
0
You definitely can do it. I'm guessing that there is something wrong within @implementation UIColor(Hex)
你一定能做到。我猜@implementation UIColor(Hex)有问题
#5
0
Have you import'd the .h file in the class you are calling this method? Also, check if all .m files are in the target.
您是否在正在调用此方法的类中导入了.h文件?另外,检查所有.m文件是否在目标文件中。
#6
0
Hm. After changing the build configuration to release and back to debug, it worked like a charm. Strange.
嗯。在将构建配置更改为发布配置并返回到debug之后,它的工作效果非常好。奇怪。
#7
0
I know it's late but JiC it helps anyone else tearing their hair out for hours, if you are using CocoaPods in the workspace and have specified the use_frameworks directive this seems to have an effect on whether Category source code in other Frameworks gets loaded.
我知道已经很晚了,但是JiC可以帮助其他任何人在工作空间中使用CocoaPods并指定use_frameworks指令,这似乎会影响其他框架中的类别源代码是否被加载。