不允许使用swift类的Objective C头文件中定义的Constant.架构armv7的未定义符号 [英] Not allowing to use Constant defined in Objective C header file in swift class. Undefined symbols for architecture armv7
问题描述
我创建了 Objective C Header文件.并在其中添加了一些属性.
我宣布
static NSString* const kColor005C98 = @"005C98";
在Constants.h
文件
I created Objective C Header file. and added some properties in it.
i declared
static NSString* const kColor005C98 = @"005C98";
in Constants.h
file
我在 Bridging-Header 文件中将此文件定义为#import "Constants.h"
现在,当我想在某些 swift 文件中使用此属性kColor005C98
时,构建失败,并且我得到
I defined this file in Bridging-Header file as #import "Constants.h"
Now when i want to use this property kColor005C98
in some swift file it failed the build and i am getting
体系结构armv7的未定义符号:"_ kColor005C98",引用自:
Undefined symbols for architecture armv7: "_kColor005C98", referenced from:
我不知道我还需要做什么,所以我不会收到此错误? (我已经在其他目标C 文件中成功使用了此属性,在这种情况下没有问题)
i don't know what else i need to do so i don't get this error? (i have used this property in other objective C file successfully and no issue in that case)
推荐答案
更新:
从Swift 2/Xcode 7及更高版本开始,像这样的静态常量定义
As of Swift 2/Xcode 7 and later, a static constant definition like
static NSString* const kColor005C98 = @"005C98"; // in Constants.h file
导入到Swift中,可以毫无问题地使用.
is imported to Swift and can be used without problems.
(Swift 1.x的旧答案)代码出现时
static NSString* const kColor005C98 = @"005C98"; // in Constants.h file
由 Objective-C 编译器处理,它被视为两件事 合并成一条语句:
is processed by an Objective-C compiler, it is treated as two things combined into one statement:
- 一个变量 declaration ,它引入一个标识符并描述其类型,并且
- 一个变量定义,它实际上实例化/实现了该标识符.
- A variable declaration which introduces an identifier and describes its type, and
- a variable definition which actually instantiates/implements this identifier.
例如 定义与声明之间有什么区别? 很好地说明了声明和 定义.
See for example What is the difference between a definition and a declaration? for a good explanation of the difference between declaration and definition.
Swift编译器仅将该语句视为声明. 因此,该变量未在任何地方定义,从而导致链接器错误.
The Swift compiler treats the statement only as a declaration. Therefore the variable is not defined anywhere, causing the linker error.
要解决该问题,必须将定义移至Objective-C 文件:
To solve the problem, you have to move the definition to an Objective-C file:
// Constants.m:
#import "Constants.h"
NSString * const kColor005C98 = @"005C98";
并将声明更改为extern
声明:
// Constants.h:
extern NSString * const kColor005C98;
或者,您只需删除static
修饰符即可:
Alternatively, you can just remove the static
modifier:
NSString * const kColor005C98 = @"005C98";
使其与Swift一起使用.缺点是
该行包含在多个Objective-C文件中,所有这些文件
将定义一个全局可见的符号kColor005C98
,从而导致
重复符号"链接器错误.
to make it work with Swift. The disadvantage is that when
this line is included by multiple Objective-C files, all of them
will define a globally visible symbol kColor005C98
, causing
"duplicate symbol" linker errors.
另一种替代方法是使用宏定义:
#define kColor005C98 @"005C98"
这篇关于不允许使用swift类的Objective C头文件中定义的Constant.架构armv7的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!