从Swift访问Objective-C变量/函数

时间:2022-08-24 16:15:23

Suppose I have a connector named Connector.m (written in Objective-C). And I would like to create a new connector written using Swift, named Connector.swift. I would like to access all of the variables and methods from Swift. I have create a bridging-header and write import of the header file of the Connector. But I can't access any of global variables on the Objective-C class.

假设我有一个名为Connector.m的连接器(用Objective-C编写)。我想创建一个使用Swift编写的新连接器,名为Connector.swift。我想从Swift访问所有变量和方法。我已经创建了一个bridging-header并写入了Connector的头文件的导入。但我无法访问Objective-C类的任何全局变量。

Connector.m

NSString * const kHTTP_METHOD_GET = @"GET";

Connector.swift

public class Connector: NSObject {

    var parentConnector : Connector

    override init() {
        self.parentConnector = Connector
    }

    func test() {
        print(parentConnector.kHTTP_METHOD_GET) //--> ERROR : Value of type 'Connector' has no member 'kHTTP_METHOD_GET'
    }

}

Is it possible to do this? Thanks.

是否有可能做到这一点?谢谢。

4 个解决方案

#1


1  

Add following line in Connector.h.

在Connector.h中添加以下行。

extern NSString * const kHTTP_METHOD_GET;

Include Connector.h to your bridging header file.

将Connector.h包含在桥接头文件中。

#2


0  

I believe the methods/variables in Connector.m also need to be public for it to work.

我相信Connector.m中的方法/变量也需要公开才能工作。

#3


0  

This sounds like a good use-case for the Adapter Pattern. But you should be able to access Objective-C code easily.

这听起来像适配器模式的一个很好的用例。但是您应该能够轻松访问Objective-C代码。

Make sure your bridging header file is named like this:

确保您的桥接头文件的名称如下:

{your-project-name}-Bridging-Header.h

Inside your bridging header add the following:

在您的桥接标题内添加以下内容:

#import "Connector.m"

Then you have to make sure the compiler knows about your bridging header:

然后你必须确保编译器知道你的桥接头:

Click your root project > Select your target app > Build Settings

单击根项目>选择目标应用程序>构建设置

Then scroll down until you see this:

然后向下滚动,直到看到:

从Swift访问Objective-C变量/函数

Make sure your bridging header is listed, build and you should have access to your Objective-C code.

确保列出了您的桥接头,构建并且您应该可以访问Objective-C代码。

#4


0  

Make sure you have header file like this...

确保你有这样的头文件...

{project-name}-Bridging-Header.h

Add your class file in Bridging-Header.h

在Bridging-Header.h中添加您的类文件

#import "Connector.h"

And Put your below code in Connector.h file..Because in Bridging-Header.h will only import header file

并将以下代码放在Connector.h文件中。因为在Bridging-Header.h中只会导入头文件

NSString * const kHTTP_METHOD_GET = @"GET";

to on top of @interface scope..

在@interface范围之上..

#1


1  

Add following line in Connector.h.

在Connector.h中添加以下行。

extern NSString * const kHTTP_METHOD_GET;

Include Connector.h to your bridging header file.

将Connector.h包含在桥接头文件中。

#2


0  

I believe the methods/variables in Connector.m also need to be public for it to work.

我相信Connector.m中的方法/变量也需要公开才能工作。

#3


0  

This sounds like a good use-case for the Adapter Pattern. But you should be able to access Objective-C code easily.

这听起来像适配器模式的一个很好的用例。但是您应该能够轻松访问Objective-C代码。

Make sure your bridging header file is named like this:

确保您的桥接头文件的名称如下:

{your-project-name}-Bridging-Header.h

Inside your bridging header add the following:

在您的桥接标题内添加以下内容:

#import "Connector.m"

Then you have to make sure the compiler knows about your bridging header:

然后你必须确保编译器知道你的桥接头:

Click your root project > Select your target app > Build Settings

单击根项目>选择目标应用程序>构建设置

Then scroll down until you see this:

然后向下滚动,直到看到:

从Swift访问Objective-C变量/函数

Make sure your bridging header is listed, build and you should have access to your Objective-C code.

确保列出了您的桥接头,构建并且您应该可以访问Objective-C代码。

#4


0  

Make sure you have header file like this...

确保你有这样的头文件...

{project-name}-Bridging-Header.h

Add your class file in Bridging-Header.h

在Bridging-Header.h中添加您的类文件

#import "Connector.h"

And Put your below code in Connector.h file..Because in Bridging-Header.h will only import header file

并将以下代码放在Connector.h文件中。因为在Bridging-Header.h中只会导入头文件

NSString * const kHTTP_METHOD_GET = @"GET";

to on top of @interface scope..

在@interface范围之上..