Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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 (Interface)

import UIKit

internal class ViewController : UIViewController {

    override internal func viewDidLoad()

    override internal func didReceiveMemoryWarning()
}
like image 244
fs_tigre Avatar asked Jan 07 '16 02:01

fs_tigre


People also ask

What is the ViewController in Swift?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.

How do I link my storyboard to ViewController?

Select the shake button in Interface Builder. Then hold down the control button ( ⌃ ) and click-drag from the storyboard button into your ViewController.

What is a view controller iOS?

A view controller acts as an intermediary between the views it manages and the data of your app. The methods and properties of the UIViewController class let you manage the visual presentation of your app. When you subclass UIViewController , you add any variables you need to manage your data in your subclass.


2 Answers

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

  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. 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. 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).

like image 87
Nicolas Miari Avatar answered Oct 19 '22 21:10

Nicolas Miari


In swift you don't have separate interface (or header files) and implementation files. 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. Please refer this documentation-

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

like image 29
Shripada Avatar answered Oct 19 '22 19:10

Shripada