将appdelegate从目标c扩展到swift

    科技2025-02-26  5

    If your AppDelegate is written in Objective-C and you are looking to write an extension to support Swift, this article is for you..

    如果您的AppDelegate是用Objective-C编写的,并且您正在寻找写扩展以支持Swift,那么本文适合您。

    步骤1: (Step One:)

    Create a new swift file — example, AppDelegateExtension.swift , and add the following code snippet.

    创建一个新的swift文件—例如AppDelegateExtension.swift ,并添加以下代码片段。

    AppDelegateExtension.swift AppDelegateExtension.swift

    Note: Don’t forget to include public access specifier, else methods inside extension cannot be accessed in Objective-C file.

    注意:不要忘了包括公共访问说明符,否则在Objective-C文件中不能访问扩展内部的方法。

    第二步: (Step Two:)

    In your AppDelegate.m, you can call extension methods. See the below code snippet

    在您的AppDelegate.m中,您可以调用扩展方法。 参见下面的代码片段

    AppDelegate.m AppDelegate.m

    第三步: (Step Three:)

    In your AppDelegate.m, add the following header.

    在您的AppDelegate.m中,添加以下标头。

    import "Objective C generated Interface Header Name"

    You can get this value from: Build Settings -> Swift Compiler General

    您可以从以下位置获取此值: Build Settings-> Swift Compiler General

    That’s it folks.. hope you like this. :)

    就是这样..希望你喜欢这个。 :)

    翻译自: https://medium.com/macoclock/extend-appdelegate-from-objective-c-to-swift-e0b4127c30d1

    Processed: 0.012, SQL: 8