gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:13:04 24 4
gpt4 key购买 nike

此代码来自 Swift 项目 App 委托(delegate)。它用于帮助使用可发布的 key 配置 Stripe。

//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
...
}
将 Swift 行添加到 Objective C App Delegate 后构建应用程序时显示两个错误
//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'
这是在 @objc 之前编译时出现的类似错误。已添加到演示 Swift 函数 MockApiClient。它应该添加到其他地方吗?我试过添加 @objc到这里的答案中提到的枚举无济于事。
//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
}
采取的步骤:
  • 打开 Objective C 项目并创建一个桥接头
  • 在仍然在 Obj C 项目中时在 Swift 中创建了一个演示类,以确保可以使用它,在这种情况下,在加载 View 时从 Objective C 文件中打印。具体派生自一个 NSObject。将覆盖添加到初始化程序并使用 @objc字首。
    //  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"导入自动生成的project-Bridging-Header.h文件以将其中的Objective C公开给swift
  • 将必要的 Swift 文件添加到 Objective C 项目,以便 Constants.publishablekey来自 Constants.swift 的数据能够被找到的

  • 如何将此 Swift App 委托(delegate)代码添加到 Objective C 项目的 App 委托(delegate)中?
    编辑:添加 @objc 时出错到 enum Constants.swift 中的声明
    enter image description here

    最佳答案

    Edit: error when adding @objc to the enum declaration in 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() {}
    }

    关于ios - 如何从 Objective C 项目 App Delegate 调用 Swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65854409/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com