unrecognized selector sent to class 0x102265ad8’ 类扩展的坑
- 个人知识眼界有限,仅提供思路。考虑不周,思路不正确望指正。
错误现象:unrecognized selector sent to class 0x102265ad8’
错误原因:Other Linker Flags 链接设置错误导致类扩展不知道如何正确读取,找不到类方法而产生的错误。eg:我的出现错误
Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘+[ShareSDK showShareActionSheet:items:shareParams:onShareStateChanged:]: unrecognized selector sent to class 0x102ad5ad8’
解释:原因是找不到实现的方法类。在我写sharesdk的类扩展时,系统没有识别[ShareSDK showShareActionSheet:items:shareParams:onShareStateChanged:]方法是类扩展中的方法还是 sharesdk的方法,它会傻傻的去sharesdk中找方法。然而找不到,就崩溃啦(crash)。。
解决办法:让系统找的到方法。通过Other Linker flag 设置添加某一个文件的链接库,或者在other linker flag 中添加$(inherited)。
$(inherited)子类复写父类同名方法时,Inherited; 可以在子类此方法中保留父类同名方法的动作;如父类方法showinfo中有一个ShowMessage(‘Is Father’); 则在子类中复写此方法时,若加Inherited;,则其执行时到Inherited;处时会跳出’Is Father’框,即进入了父类同名方法,执行完再回来接着执行子类方法中动作;若不用Inherited;,那么就不会跳出’Is Father’框而只会执行子类方法中动作。说的多态在此表现更强,即代码复用功能,一个Inherited;就继承的父类同名方法的所有动作,子类复写再加强其功能即可
此部分出错和代码关系不大,主要是设置targets的building setting 的Other Linker flag 设置错误。
-all_load Loads all members of static archive libraries.
-ObjC Loads all members of static archive libraries that implement an Objective-C class or category.
-force_load (path_to_archive) Loads all members of the specified static archive library. Note: -all_load forces all members of all archives to be loaded. This option allows you to target a specific archive.
翻译过来就是-all_load就是会加载静态库文件中的所有成员,-ObjC就是会加载静态库文件中实现一个类或者分类的所有成员,-force_load(包的路径)就是会加载指定路径的静态库文件中的所有成员。所以对于使用runtime时候的反射调用的方法应该使用这三个中的一个进行link,以保证所有的类都可以加载到内存中供程序动态调用