Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从目标C项目应用程序代表处调用Swift?_Ios_Swift_Iphone_Xcode_Cocoa Touch - Fatal编程技术网

Ios 如何从目标C项目应用程序代表处调用Swift?

Ios 如何从目标C项目应用程序代表处调用Swift?,ios,swift,iphone,xcode,cocoa-touch,Ios,Swift,Iphone,Xcode,Cocoa Touch,此代码来自Swift项目应用程序代表。它用于帮助配置带有可发布密钥的条带 //Appdelegate.swift func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //The code helps configure Stripe with a publis

此代码来自Swift项目应用程序代表。它用于帮助配置带有可发布密钥的条带

//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行添加到目标C应用程序代表后,在构建应用程序时显示两个错误

//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
添加到enum中,如这里的答案中所述,但没有任何效果

//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项目并创建桥接标头

  • 在Swift中创建了一个演示类,同时仍在Obj C项目中,以确保可以使用该类,在本例中,在加载视图时从目标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”
    导入复制到自动生成的
    项目桥接头.h
    文件中,以将其中的目标C公开给swift

  • 将必要的Swift文件添加到Objective C项目中,以便可以找到来自
    Constants.Swift
    Constants.publishablekey
    数据

  • 如何将此Swift应用程序委派代码添加到目标C项目的应用程序委派中

    编辑:将
    @objc
    添加到
    常量中的
    枚举
    声明时出错。swift


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

    然后,你的Swift物品需要进入该文件。要实现这一点,它们必须是Objective-C完全可以看到的类型(即类),并且需要使用适当的
    @objc
    属性显式地暴露给Objective-C

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

    用作命名空间的Swift枚举不能向Objective-C公开。 您可能需要使用
    使其同时适用于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() {}
    }
    

    如果STPPaymentConfiguration和Constants是Swift对象,那么要让Objective-C看到,它们必须是Objective-C可以看到的类型(即使用
    @objc
    公开的类),Objective-C文件需要导入自动生成的头文件(与“添加”头文件无关)。感谢@matt的响应。请确认正确的步骤是创建桥接头文件,将
    #导入“YourProject Swift.h”
    添加到Appdelegate.m,将
    #导入“Stripe.h”
    添加到桥接头文件,因为它包含一个包含所有条带头文件的.m文件,然后将
    Appdelegate.h
    添加到桥接头文件,然后将Swift行添加到
    Appdelegate.m
    didfishlaunchingwithoptions
    方法,然后添加
    @objc
    前缀?具体来说,我已经尝试了@objc枚举常量。。。在Constants.swift中,编译器警告显示:“@objc”enum必须声明一个整数raw typeYes,你读了我说的吗?我很清楚C能看到什么。我提到过枚举吗?我说的不是桥牌头球。请看我说的话,谢谢。按照建议更新
    Constants.Swift
    文件后,错误
    使用未声明的标识符“Constants”
    消失了,因此知道应用程序似乎正在正式读取常量属性。现在,在类型为“STPPaymentConfiguration”的对象上只发现一个
    属性“shared”错误。有什么想法吗
    委托中使用的
    STPPaymentConfiguration
    类在条带Cocoapods的
    STPPaymentConfiguration.h
    中声明。考虑到这个类已经用Objective C编写,如何进一步包含它呢?就我查看Stripe的文档页面而言,所有类型、属性和方法都已经标记为
    @objc
    。但其中一些被赋予了独立于Swift名称的Objective-C名称。检查文档。