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

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) 。注意:仅当您重建项目时,才会更新生成的接口文件。

Image for post
Image for post

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。

Image for post

结论 (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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值