Ios 如何从目标C项目应用程序代表处调用Swift?
此代码来自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
//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
}
采取的步骤:
@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
Constants.Swift
的Constants.publishablekey
数据
@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名称。检查文档。