ViewController.swift (Interface) 文件是什么 - 在 Counterparts 中 [英] What is the ViewController.swift (Interface) file for - in Counterparts

查看:26
本文介绍了ViewController.swift (Interface) 文件是什么 - 在 Counterparts 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到,当我创建第一个 ViewController 时,Xcode 自动创建了一个名为 ViewController.swift (Interface) 的文件.

I just noticed that a file called ViewController.swift (Interface) was created automatically by Xcode when I created my first ViewController.

Swift 中的类是否具有/需要与 Objective-C 中相同的接口并由 Xcode 在幕后创建?

Do classes in Swift have/need interfaces the same as in Objective-C and are created behind the scenes by Xcode?

谁能这么好心解释一下这个文件的用途是什么?

Can someone be so kind and explain what is the purpose of this file?

ViewController.swift(接口)

import UIKit

internal class ViewController : UIViewController {

    override internal func viewDidLoad()

    override internal func didReceiveMemoryWarning()
}

推荐答案

在 C 和 Objective-C 中,你有单独的 interface (.h) 和 implementation (.c, .m) 文件.

In C and Objective-C, you have separate interface (.h) and implementation (.c, .m) files.

  1. 接口文件可以快速查看其他代码可用的该类的所有方法和属性(前提是它们导入了接口文件).它就像一本书的章节索引.
  2. 实现,另一方面,是编写实际方法代码的地方.
  1. The interface file gives a quick view of all the methods and properties of that class that are available to other code (provided they import the interface file). It is like the chapter index of a book.
  2. The implementation, on the other hand, is where the actual method code is written.

在 Swift 中,另一方面,没有这样的区别,并且类会自动对您的所有代码可用.好处当然是,你的项目中源文件的数量大约减少了一半,而且你不需要在编码时在接口文件和实现文件之间来回走动(全部在一个地方")).

In Swift, on the other hand, there is no such distinction and classes are automatically available to all your code. The advantage is, of course, that the number of source files in your project is roughly cut in half, and you don't need to go back and forth between the interface file and implementation file while coding (it's all "in one place").

因此,为了弥补仅接口"文件(如 C/Objective-C 中的头文件)的缺失,Xcode 生成了一个动态"的文件,目的是向您展示可用的方法和该类的属性.您会注意到缺少方法体,以及声明为 private 的属性和方法(因为它们在类之外是不可访问的,因此根据定义不是接口"的一部分).

So, to make up for the lack of an "interface-only" file (like the headers in C/Objective-C), Xcode generates one "on the fly" just for the purpose of showing you the available methods and properties of that class. You will notice that method bodies are missing, as well as properties and methods declared as private (because they are inaccessible outside of the class, hence by definition not part of the "interface").

它不是驻留在文件系统中任何位置的真实文件(据我所知).

It is not a real file that resides anywhere in your file system (as far as I know).

这篇关于ViewController.swift (Interface) 文件是什么 - 在 Counterparts 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆