What is ViewController.swift (Interface) file for - in Counterparts

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

Classes in Swift have / need interfaces in the same way as in Objective-C and are created behind the scenes in Xcode?

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

ViewController.swift (interface)

 import UIKit internal class ViewController : UIViewController { override internal func viewDidLoad() override internal func didReceiveMemoryWarning() } 
+5
source share
2 answers

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

  • An interface file provides a quick overview of all the methods and properties of this class that are available for other code (when importing an interface file). This is similar to the index of a book chapter.
  • Implementation , on the other hand, is where the code of the actual method is written.

In Swift , on the other hand, there is no such difference, and classes are automatically available for all of your code. Of course, the advantage is that the number of source files in your project is approximately halved, and you do not need to go back and forth between the interface file and the implementation file during encoding (all this is β€œin one place”,).

So, to make up for the lack of "only an interface" (for example, headers in C / Objective-C), Xcode generates one on the fly just to show you the available methods and properties of this class. You will notice that there are no method objects, as well as properties and methods declared private (because they are not accessible outside the class, therefore, by definition they are not part of the "interface").

This is not a real file that is located anywhere on your file system (as far as I know).

+9
source

In swift, you do not have a separate interface (or header files) and implementation files. You simply create the class interface and implementation together in one file. Other parts of the project will automatically gain access to the type entered in this file.

Now, by going to ViewController.Swift, this is the file that contains the logic of your controller level. Please refer to this documentation -

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

+1
source

Source: https://habr.com/ru/post/1240040/


All Articles