简体   繁体   English

如何将AppDelegate Swift代码放入Objective-C AppDelegate文件并使其工作?

[英]How do I put AppDelegate Swift code into Objective-C AppDelegate file and make it work?

I'm trying to work with an Amazon Web Serivces SDK for iOS called AWSAppSync. 我正在尝试使用名为AWSAppSync的适用于iOS的Amazon Web Serivces SDK。 I found good instructions for Swift . 我找到了Swift的好指示 I'll try to make the question more general to all Obj-c/Swift bridging problems: 我会尝试让所有Obj-c / Swift桥接问题更加通用:

There's a code snippet for the AppDelegate.Swift file. 有一个AppDelegate.Swift文件的代码片段。 However, my AppDelegate is in Objective-C. 但是,我的AppDelegate在Objective-C中。 I've put it into a function (called awsConfig) within a Swift file, referenced the file in the Bridging-Header.h and called the function in the Swift file from my AppDelegate.m: 我把它放在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;
}

And here's the snippet within the Swift file. 这是Swift文件中的片段。

// 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)")
    }

When I want to use it in another Swift file, like this: 当我想在另一个Swift文件中使用它时,如下所示:

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appSyncClient = appDelegate.appSyncClient

I get the error: 我收到错误:

(!) Value of type 'AppDelegate' has no member 'appSyncClient' (!)'AppDelegate'类型的值没有成员'appSyncClient'

Why doesn't this work? 为什么这不起作用? And what would be a better way to tackle this problem if my workaround is too ugly? 如果我的解决方法太难看,那么解决这个问题的更好方法是什么?

this might be because of the version you currently are working with since this issue can relate to the problem of the versions you are using. 这可能是因为您当前正在使用的版本,因为此问题可能与您正在使用的版本的问题有关。 So try to use a different version first since this happened to me once and i just used a old version since there are slight changes to the compiler so if you can try different versions and then let me know and make sure to make a report of the versions you tried that failed. 所以首先尝试使用不同的版本,因为这发生在我身上一次,我只使用旧版本,因为编译器有轻微的变化,所以如果你可以尝试不同的版本然后让我知道并确保报告的您尝试失败的版本。

I found the answer to be a slight error: Turns out I put appDelegate.h instead of appDelegate.m in my bridging-header file. 我发现答案是一个小错误:原来我把appDelegate.h而不是appDelegate.m放在我的bridging-header文件中。 After changing that it works. 改变它后,它的工作原理。 Probably translating the Swift code to objective C would've been a better option, but this is easier if you're not as skilled in both languages like I am. 将Swift代码翻译成目标C可能是一个更好的选择,但如果你不像我这样熟练使用这两种语言,这就更容易了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM