如何从 Objective C 项目 App Delegate 调用 Swift?

问题描述

此代码来自 Swift 项目应用程序委托。它用于帮助使用可发布密钥配置 Stripe。

Id : 3
Id : 5

将 Swift 行添加到 Objective C App Delegate 后构建应用程序时显示两个错误

//Appdelegate.swift
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: 
[UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{
//The code helps configure Stripe with a publishable key.
STPPaymentConfiguration.shared().publishableKey = Constants.publishableKey
...
}

这是在将 //AppDelegate.h - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { STPPaymentConfiguration.shared().publishableKey = Constants.publishableKey Property 'shared' not found on object of type 'STPPaymentConfiguration' Use of undeclared identifier 'Constants' 添加到演示 Swift 函数 MockApiClient 之前编译时出现的类似错误。是否应该在其他地方添加?我已经尝试将 @objc 添加到此处的答案中提到的枚举中,但无济于事。

@objc

采取的步骤:

  1. 打开 Objective C 项目并创建桥接头

  2. 在 Swift 中创建了一个演示类,同时还在 Obj C 项目中,以确保它可以使用,在这种情况下,在加载视图时从目标 C 文件打印。特别是从 NSObject 派生的。将覆盖添加到初始值设定项并使用 //Constants.swift //This is the file the original Swift app delegate accesses import Foundation enum Constants { static let publishableKey = "pk_live_..." static let baseURLString = "http://54.33.123.227:1234" static let defaultCurrency = "usd" static let defaultDescription = "Receipt" //change to describe actual app & charge } 前缀。

    @objc
  3. // MockApiClient.swift import Foundation class MockApiClient: NSObject { override init() { print("Initializer called in Mock API client") } @objc func executeRequest() { print("The execute request has been called in the Mock API Client") } } //ViewController.h //Prints the Swift request written in the MockApiClient the the view loads @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view,typically from a nib. MockApiClient *client = [MockApiClient new]; [client executeRequest]; } 导入复制到自动生成的 #import "ViewController.h" 文件,以将其中的 Objective C 公开给 swift

  4. 向目标 C 项目添加了必要的 Swift 文件,以便可以找到来自 project-Bridging-Header.hConstants.publishablekey 数据

如何将这个 Swift App 委托代码添加到 Objective C 项目的 App 委托中?

编辑:将 Constants.swift 添加到 @objc 中的 enum 声明时出错

enter image description here

解决方法

编辑:将@objc 添加到 Constants.swift 中的枚举声明时出错

用作命名空间的 Swift 枚举不能暴露给 Objective-C。 您可能需要使用 class 使其同时适用于 Swift 和 Objective-C:

@objcMembers
class Constants: NSObject {
    static let publishableKey = "pk_live_..."
    static let baseURLString = "http://54.33.123.227:1234"
    static let defaultCurrency = "usd"
    static let defaultDescription = "Receipt" //change to describe actual app & charge
    
    private override init() {}
}
,

Objective-C 查看 Swift 中定义的东西的能力取决于自动生成的头文件。这不是桥接头。它是一个隐藏在您的派生数据中的头文件,名为 YourProject-Swift.h。您的 Objective-C .m 文件需要 #import "YourProject-Swift.h"(使用正确的名称)。

然后,您的 Swift 内容需要进入该文件。为此,它们必须是 Objective-C 完全可以看到的类型(即类),并且需要使用适当的 @objc 属性显式地暴露给 Objective-C。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...