简体   繁体   English

访问 Objective-C #define constant from Swift

[英]Accessing Objective-C #define constant from Swift

I have an existing project with Swift and Objective C.h and.m files.我有一个现有项目,其中包含 Swift 和 Objective C.h 和.m 文件。 In one of the.h files are numerous在其中一个.h 文件中有很多

#define kSomeConstant @"some string" #define kSomeConstant @"一些字符串"

How can I make these available to Swift files in the project when the Swift file is in a framework that is a subproject of the app?当 Swift 文件位于作为应用程序子项目的框架中时,如何使这些文件可用于项目中的 Swift 文件?

So far I've tried making wrapper static methods in the.m file class definition such as:到目前为止,我已经尝试在 .m 文件 class 定义中制作包装器 static 方法,例如:

+ (NSString*) objc_kSomeConstant { return kSomeConstant; + (NSString*) objc_kSomeConstant { 返回 kSomeConstant; } }

And I've tried doing something like the following in the.h:我已经尝试在.h中做类似以下的事情:

extern NSString *const kSomeConstant; extern NSString *const kSomeConstant;

with the definition in the.m file:使用 .m 文件中的定义:

NSString *const kSomeConstant = @"some string" NSString *const kSomeConstant = @"某个字符串"

Neither way has worked.两种方法都没有奏效。

To be clear, you have some definitions in an Objective-C header file in the main application target of your project, and you want to make them available to a Swift file in a framework target which is a subproject of your main project.需要明确的是,您在项目的主应用程序目标中的 Objective-C header 文件中有一些定义,并且您希望将它们提供给 Swift 文件,该文件是项目框架目标中的主项目子文件。 Normally your application depends on definitions in the framework, not the other way around.通常,您的应用程序取决于框架中的定义,而不是相反。 To share these definitions between the application and the framework, create a clang compiler module that can be used by both.要在应用程序和框架之间共享这些定义,请创建一个可供两者使用的 clang 编译器模块

To do this, move the header with the definitions into a folder (which we'll call "Common"), and in this folder create a file called "module.modulemap" which contains the following:为此,请将带有定义的 header 移动到一个文件夹(我们将其称为“Common”),并在该文件夹中创建一个名为“module.modulemap”的文件,其中包含以下内容:

module Common {
    header "CommonDefinitions.h"
}

Next, in your framework target, set the Import Paths setting (under Swift Compiler - Search Paths ) to the path of this folder ( eg , $(SRCROOT)/Common ).接下来,在您的框架目标中,将Import Paths设置(在Swift Compiler - Search Paths下)设置为该文件夹的路径(例如$(SRCROOT)/Common )。 You may need to add this folder to the Header Search Paths setting of your main application as well, so that it will continue to compile successfully.您可能还需要将此文件夹添加到主应用程序的Header 搜索路径设置中,以便继续成功编译。

Now the definitions can be accessed from the Swift files in the framework by simply adding the statement import Common .现在,只需添加语句import Common即可从框架中的 Swift 文件访问定义。 (Note that names #define d as Objective-C string literals, like kSomeConstant in your example, are imported into Swift as String constants; there should be no need to rewrite them as extern NSString * declarations.) (请注意,名称#define d 为 Objective-C 字符串文字,如示例中的kSomeConstant ,作为String常量导入 Swift ;不需要将它们重写为extern NSString *声明。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM