您的位置:首页 > 移动开发 > Swift

使用Xcode生成的接口了解Objective-C与Swift的互操作性

2020-07-26 12:20 1051 查看

Did you know that Xcode has a limited built-in Objective-C → Swift converter?

您是否知道Xcode具有有限的内置Objective-C→Swift转换器?

Typically, for every Objective-C class, we have two files: a header file and an implementation file. The header file (or interface) contains the public properties, methods, and functions of the class, while the implementation file (as the name implies) contains the internal implementation details of the class methods and functions as well as private properties, methods and functions.

通常,对于每个Objective-C类,我们都有两个文件:标头文件和实现文件。 头文件(或接口)包含类的公共属性,方法和函数,而实现文件(顾名思义)包含类方法和函数的内部实现详细信息以及私有属性,方法和函数。

In Swift the header and the implementation files are combined into a single .swift file. The access scope if the properties, functions and mrthods is completely defined by access specifiers with the default access level being “internal”.

在Swift中,标头和实现文件被组合为单个.swift文件。 属性,功能和方法的访问范围完全由访问说明符定义,默认访问级别为“内部”。

Excited to dig deeper?

兴奋地深入研究?

To see this automatic converter in action, please download this sample Xcode project and open the Animal.h file.

要查看此自动转换器的运行情况,请下载此示例Xcode项目并打开Animal.h文件。

To see the converter in action just build your project (⌘ + B).Note: the generated interface files are only updated only when you rebuild your project.

要查看运行中的转换器,只需构建您的项目(⌘+ B) 。注意:仅当您重建项目时,才会更新生成的接口文件。

Here is the Generated Swift Inerface for the Animal.h file

这是Animal.h文件的生成的Swift Inerface

One more small detail, you can also convert your Swift files to Objective-C.

还有一个小细节,您还可以将Swift文件转换为Objective-C。

结论 (Conclusion)

This feature helps us better understand how Xcode imports any given Objective-C declaration in Swift. When using Swiftify to gradually migrate your projects, you can use this feature to understand any nuances in the syntax of declarations. But as you have already seen this method converts only the function declarations and not the code inside the functions and methods.

此功能有助于我们更好地了解Xcode如何在Swift中导入任何给定的Objective-C声明。 使用Swiftify逐步迁移项目时,可以使用此功能来理解声明语法中的任何细微差别。 但是,正如您已经看到的那样,此方法仅转换函数声明,而不转换函数和方法中的代码。

翻译自: https://medium.com/swiftify/understanding-objective-c-swift-interoperability-xcode-generated-interfaces-5e73a5e3a4ec

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: