【发布时间】:2019-03-28 16:56:09
【问题描述】:
我正在尝试使用名为 AWSAppSync 的适用于 iOS 的 Amazon Web Serivces 开发工具包。我找到了good instructions for Swift。 我会尽量让这个问题更普遍地适用于所有 Obj-c/Swift 桥接问题:
AppDelegate.Swift 文件有一个代码 sn-p。但是,我的 AppDelegate 在 Objective-C 中。我已将它放入 Swift 文件中的一个函数(称为 awsConfig)中,在 Bridging-Header.h 中引用该文件,并从我的 AppDelegate.m 调用 Swift 文件中的函数:
// AppDelegate.m
#import "<ProductModuleName>-Swift.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
appsyncer *syncClass = [appsyncer new];
[syncClass awsConfig];
return YES;
}
这是 Swift 文件中的 sn-p。
// SwiftAppDelegateExtension.Swift
var appSyncClient: AWSAppSyncClient?
do {
// You can choose the directory in which AppSync stores its persistent cache databases
let cacheConfiguration = try AWSAppSyncCacheConfiguration()
// AppSync configuration & client initialization
let appSyncServiceConfig = try AWSAppSyncServiceConfig()
let appSyncConfig = try AWSAppSyncClientConfiguration(appSyncServiceConfig: appSyncServiceConfig,
cacheConfiguration: cacheConfiguration)
appSyncClient = try AWSAppSyncClient(appSyncConfig: appSyncConfig)
} catch {
print("Error initializing appsync client. \(error)")
}
当我想在另一个 Swift 文件中使用它时,像这样:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appSyncClient = appDelegate.appSyncClient
我得到错误:
(!)“AppDelegate”类型的值没有成员“appSyncClient”
为什么这不起作用?如果我的解决方法太难看,还有什么更好的方法来解决这个问题?
【问题讨论】:
-
为什么不直接把 Swift 代码翻译成 Objective C 呢?
标签: ios swift appdelegate bridging-header