简体   繁体   English

什么是ViewController.swift(接口)文件 - 在Counterparts中

[英]What is the ViewController.swift (Interface) file for - in Counterparts

I just noticed that a file called ViewController.swift (Interface) was created automatically by Xcode when I created my first ViewController. 我刚刚注意到,当我创建第一个ViewController时,Xcode会自动创建一个名为ViewController.swift (Interface)的文件。

Do classes in Swift have/need interfaces the same as in Objective-C and are created behind the scenes by Xcode? Swift中的类是否需要与Objective-C中相同的接口,并且是由Xcode在幕后创建的?

Can someone be so kind and explain what is the purpose of this file? 有人可以这么善良并解释这个文件的目的是什么?

ViewController.swift (Interface) ViewController.swift(接口)

import UIKit

internal class ViewController : UIViewController {

    override internal func viewDidLoad()

    override internal func didReceiveMemoryWarning()
}

In C and Objective-C, you have separate interface (.h) and implementation (.c, .m) files. 在C和Objective-C中,您有单独的接口 (.h)和实现 (.c,.m)文件。

  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. 另一方面, 实现是编写实际方法代码的地方。

In Swift , on the other hand, there is no such distinction and classes are automatically available to all your code. 另一方面, 在Swift中 ,没有这样的区别,并且所有代码都可以自动使用类。 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"). 当然,优点是项目中的源文件数量大致减少了一半,并且在编码时不需要在接口文件和实现文件之间来回切换(它们都在“一个地方”) )。

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

It is not a real file that resides anywhere in your file system (as far as I know). 它不是一个存在于文件系统中任何位置的真实文件(据我所知)。

In swift you don't have separate interface (or header files) and implementation files. 在swift中,您没有单独的接口(或头文件)和实现文件。 You just create the class interface and implementation together in the same file. 您只需在同一文件中一起创建类接口和实现。 Other parts of the project, will automatically get access to the type introduced in a given file. 项目的其他部分,将自动访问给定文件中引入的类型。

Now coming to the ViewController.Swift, this is the file where your view's controller layer logic goes. 现在来到ViewController.Swift,这是视图的控制器层逻辑所在的文件。 Please refer this documentation- 请参阅此文件 -

https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/

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

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