今天早些时候,我尝试将单元测试添加到我的xcode项目中,以创建一个静态框架。在将单元测试目标添加到我的项目后,我能够成功运行单元测试模板并让测试捕获默认断言。然后,在将我想测试的文件导入到新的SenTestCase子类中之后,我尝试运行测试,但由于链接器错误,我的UIColor类别由于测试而失败。链接器错误的文本如下:UIColor类别链接器错误
Undefined symbols for architecture i386:
"_CGColorGetComponents", referenced from:
-[UIColor(ColorExtension) hexValue] in StaticFramework(StaticFramework)
"_CGColorGetNumberOfComponents", referenced from:
-[UIColor(ColorExtension) hexValue] in StaticFramework(StaticFramework)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
接下来,我发现这两个引用在我的项目中。都住在我的类别下的作用下:
- (NSString *)hexValue
{
UIColor *color = self;
const size_t totalComponents = CGColorGetNumberOfComponents(color.CGColor);
const CGFloat * components = CGColorGetComponents(color.CGColor);
return [NSString stringWithFormat:@"#%02X%02X%02X",
(int)(255 * components[MIN(0,totalComponents-2)]),
(int)(255 * components[MIN(1,totalComponents-2)]),
(int)(255 * components[MIN(2,totalComponents-2)])];
}
如果我没有记错,这两个都是NSColor类的一部分。随着更多的检查,我还注意到,在我的单元测试框架的文件夹中,UIKit.framework & Foundation.framework是红色的。我试图重新导入它们并构建,但这并不能解决这个问题。
有趣的是,只要没有运行关联的Test,我的静态框架仍然可以正常构建和运行。当我评论这个函数(每次使用它)时,单元测试都没有问题。假设缺失的UIKit.framework & Foundation.framework可能导致链接程序未找到NSColor属性,我会正确吗?有没有人有任何额外的建议,可以解释为什么这些属性导致这些问题或我可以做些什么来解决它们?
任何帮助将不胜感激。感谢您的帮助。
您是否将'CoreGraphics.framework'链接到您的项目? – rmaddy 2013-05-01 18:57:32
不,我没有在我的静态框架或我的单元测试中使用CoreGraphics.framework。 – Krejko 2013-05-01 19:00:59
是的,你做到了。这两个'CGColor ...'函数来自CoreGraphics。您需要将'CoreGraphics.framework'链接到您的项目。这将解决链接器错误。 – rmaddy 2013-05-01 19:02:39