本文主要是介绍XCode中Categories的类出现无法识别函数的问题的解决办法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
今天在工程中加入日历控件,使用一个开源的库,Kal。
将kal库加入已有工程过程中出现一个小插曲,kal工程类目录如果和工程类文件同一级的话会出现包含的头文件无法找到的问题,估计是工程无法定位外部工程在.xcodeproj下一级的目录。将kal类文件夹放在.xcodeproj文件同一级或者上一级,在头文件路径中设置好查找路径就没有问题。
下面说回主题,日历视图添加到指定为止后,调用如下代码定位到当前日期:
- (void)showAndSelectToday
{[kalVC showAndSelectDate:[NSDate date]];
}
上面函数进一步调用以下函数:
- (void)moveToMonthForDate:(NSDate *)date
{self.baseDate = [date cc_dateByMovingToFirstDayOfTheMonth];[self recalculateVisibleDays];
}
这时候问题出现了,运行时候报错 :-[__NSCFDate cc_dateByMovingToFirstDayOfTheMonth]: unrecognized selector sent to...
NSDate是已经categories过了的,定义如下:
@interface NSDate (KalAdditions)
问题就出现在这里了,为什么没调用已经categories过的类,而调用了最原始的类?对比了例子工程,所有包含文件,链接文件等都一样了,就是得不到相同的结果。
苦苦搜索了一番,原来categories过的类要使XCode识别,需要添加链接参数,在工程中设置如下:Build Settings->other linker flags加入-ObjC或者-all_load。例子工程加入的参数是-all_load。
引用老外的解释:
Objective-C does not define linker symbols for each function (or method, in Objective-C) - instead, linker symbols are only generated for each class. If you extend a pre-existing class with categories, the linker does not know to associate the object code of the core class implementation and the category implementation. This prevents objects created in the resulting application from responding to a selector that is defined in the category.
To resolve this issue, the target linking against the static library must pass the -ObjC option to the linker. This flag causes the linker to load every object file in the library that defines an Objective-C class or category. While this option will typically result in a larger executable (due to additional object code loaded into the application), it will allow the successful creation of effective Objective-C static libraries that contain categories on existing classes.
Important : For 64-bit and iPhone OS applications, there is a linker bug that prevents -ObjC from loading objects files from static libraries that contain only categories and no classes. The workaround is to use the -all_load or -force_load flags.重新编译,运行,终于看到期待已久的日历界面了。thank goddess!
这篇关于XCode中Categories的类出现无法识别函数的问题的解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!