https://developer.apple.com/library/content/qa/qa1490/_index.html

A: If you're seeing a "selector not recognized" runtime exception when calling a category method that is implemented in a static library, you are hitting the link-time build issue described here, and need to add the -ObjC linker flag to your project, by following these steps:

  1. In Xcode, choose View > Navigators > Show Project Navigator, or press ⌘1.

  2. Select your project under the PROJECT heading in the Project Navigator, then select the Build Settings tab.

  3. Scroll down to the Other Linker Flags build setting under the Linking collection, or type "Other Linker Flags" into the search bar.

  4. Set the value of the Other Linker Flags build setting to $(OTHER_LDFLAGS) -ObjC.

Figure 1  Modifying the Other Linker Flags build setting

Troubleshooting

If adding the -ObjC flag isn't fixing the problem, double check that a conflicting Target build setting is not overriding it, by following the above steps, but selecting the current target under "TARGETS" in step 2, instead of the project.

Other Causes of selector not recognized Exceptions

The most common causes of a "selector not recognized" exception are:

No Such Method

The method really does not exist. Check your spelling. Check documentation to verify that the method exists on the version of the operating system your app is using.

Memory Management

Your app is trying to use an object after it has been deallocated, use the Zombies instrument to debug this kind of problem. You are seeing "selector not recognized" because the memory has been re-allocated as a different kind of object.

What causes those exceptions?

An impedance mismatch between UNIX static libraries and the dynamic nature of Objective-C can cause category methods in static libraries to not be linked into an app, resulting in "selector not recognized" exceptions when the methods aren't found at runtime.

The Linker

When a C program is compiled, each "source file" is turned into an "object file" that contains executable functions and static data. The linker glues these object files together into a final executable. That executable is eventually bundled into an app by Xcode.

When a source file uses something (like a function) defined in another file, then an undefined symbol is written into the object file, to "stand in" for the missing thing. The linker resolves these symbols by pulling in the object files that include definitions of undefined symbols when building the final executable.

For example, if main.c uses the function foo(), where foo is defined in another file, B.c, then the object file main.o will have an unresolved symbol for foo(), and B.o will include an implementation of foo(). At link time, B.o will be brought into the final executable, so that the code in main.o now references the implementation of foo() defined in B.o.

A UNIX static library is just a collection of object files. Normally the linker only pulls in an object file from a static library if doing so would resolve some undefined symbol. Not pulling in all object files reduces the size of the final executable.

Objective-C

The dynamic nature of Objective-C complicates things slightly. Because the code that implements a method is not determined until the method is actually called, Objective-C does not define linker symbols for methods. Linker symbols are only defined for classes.

For example, if main.m includes the code [[FooClass alloc] initWithBar:nil]; then main.o will contain an undefined symbol for FooClass, but no linker symbols for the -initWithBar:method will be in main.o.

Since categories are a collection of methods, using a category's method does not generate an undefined symbol. This means the linker does not know to load an object file defining the category, if the class itself is already defined. This causes the same "selector not recognized" runtime exception you would see for any unimplemented method.

The -ObjC Linker Flag

Passing the -ObjC option to the linker causes it to load all members of static libraries that implement any Objective-C class or category. This will pickup any category method implementations. But it can make the resulting executable larger, and may pickup unnecessary objects. For this reason it is not on by default.


How do I fix "selector not recognized" runtime exceptions when trying to use category methods from a static library?的更多相关文章

  1. Effective Java 58 Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

    Three kinds of throwables Throwables Checked Recoverable checked exceptions Y Y runtime exceptions N ...

  2. iOS的Runtime机制下给类别(category)添加属性、替换原有类的方法执行

    一.Runtime的理解 OC是面向对象的语言这是常识,其实就是通过Runtime机制动态创建类和对象,这里只是简单的运用runtime的使用! 二.类别(category)添加属性_使用前记得导入头 ...

  3. Building Objective-C static libraries with categories

    Q: How do I fix "selector not recognized" runtime exceptions when trying to use category m ...

  4. Other Linker Flags到底是什么

    一.问题描述 在项目开发中用到百度地图,有时候在工程中会报“方法找不到”的错误(unrecognized selector sent to instance). 二.问题分析 首先,要说明一下Othe ...

  5. 【转】关于Xcode的Other Linker Flags

    链接器 首先,要说明一下Other Linker Flags到底是用来干嘛的.说白了,就是ld命令除了默认参数外的其他参数.ld命令实现的是链接器的工作,详细说明可以在终端man ld查看. 如果有人 ...

  6. 关于Xcode的Other Linker Flags

    背景 在ios开发过程中,有时候会用到第三方的静态库(.a文件),然后导入后发现编译正常但运行时会出现selector not recognized的错误,从而导致app闪退.接着仔细阅读库文件的说明 ...

  7. Xcode 编辑器之关于Other Linker Flags相关问题

    一,概述 问题场景一 当从网上去下载一些之前的完整的项目的时候,用终端也 pod update了,但一运行,熟悉的linker错误就出来了. 解决办法 在Other Linker Flags(也即 O ...

  8. Selector

    原文: https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Sele ...

  9. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

随机推荐

  1. 【C++】const,static和static const类型成员变量声明及其初始化

    1)const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间 void f1() { ; cout<<x<<endl; ...

  2. 【转帖】普通程序员如何转向AI方向

    普通程序员如何转向AI方向 https://www.cnblogs.com/subconscious/p/6240151.html 眼下,人工智能已经成为越来越火的一个方向.普通程序员,如何转向人工智 ...

  3. Scala 系列(十一)—— 模式匹配

    一.模式匹配 Scala 支持模式匹配机制,可以代替 swith 语句.执行类型检查.以及支持析构表达式等. 1.1 更好的swith Scala 不支持 swith,可以使用模式匹配 match.. ...

  4. lightGBM gpu环境配置

    推荐先看一手官方的Installation Guide.我用的是ubuntu 16.04,一些要求如下图: 主要是OpenCL以及libboost两个环境的要求. (1) OpenCL的安装.我这里之 ...

  5. Oralce学习笔记(六)

    逻辑体系结构与Data file 逻辑体系结构关系图 逻辑体系结构最小单位BLOCK的研究 block能装多少行数据: drop table test_block_num purge; create ...

  6. SQL Server的常用提示

    在SQL Server中,有许多SQL语句的提示,本文总结一些比较常用的提示. OPTION LOOP/MERGE/HASH JOIN提示 该提示可以改变整个SQL语句中所有JOIN的关联算法,所以请 ...

  7. dotnet core 之 CORS使用示例

    这里列举几个经过验证的可用的CORS使用示例, 方便在需要的时候可以直接使用 示例1 #region snippet2 public void ConfigureServices(IServiceCo ...

  8. NetCore 统一处理 webapi 返回null 转为“”

    数据库中部分表字段允许空值,则代码中实体类对应的字段类型为可空类型Nullable<>,如int?,DateTime?,null值字段序列化返回的值都为null,前端对应字段赋值需要做nu ...

  9. python卸载重新安装,一键安装卸载前的所有安装的第三方插件

    好多小可爱都有这样一个困扰,python有了一个大版本的更新,我也想更新,但是安装的好多第三方的库可怎么办呀,更新之后再去一个一个的安装,那可就烦死了. 在这里我来教大家如何快速去安装python更新 ...

  10. Pika 连接 rabbitmq 集群

    原文:https://blog.csdn.net/Tech_Salon/article/details/82890431 使用 Pika 连接 rabbitmq 集群使用 python 编程经常会用到 ...