What is the difference between cocoa touch class and normal fast class?

I’ve been learning the Swift language for a while, I’m stuck in one of the main questions, that is, what is the main difference between the file templates of the Cocoa Touch class and the regular Swift class? Can anyone give any basic difference along with an example? I know that they are different because everyone has the opportunity to create a file.

+6
source share
1 answer

TL DR:. They are not different types of classes. These are just different file templates.


According to your comments:

I know that they are different because they are different options for creating a file, but they are the same.

However, they are not different.

enter image description here

We can create a "Cocoa Touch Class" or a "Swift File" (or 7 different options). "Swift Class" is not an option.

So ... with that in mind, what is the difference between these two options?

If we select Swift File , in the next dialog box we will ask you to specify a file name and choose a save location. When we click "Create" here, we simply get an empty (ish) Swift file with the name we selected.

All that gets into the file is the copyright information and the import Foundation line:

 // // File.swift // Swift_iOS // // Created by Nick Griffith on 4/18/15. // Copyright (c) 2015 nhg. All rights reserved. // import Foundation 

For the entire file to be created.


If we choose Cocoa Touch Class , we get a completely different dialogue.

We ask you to give our class a name, select its base class and select a language. In addition, if our base class is a kind of view controller, we will be given the opportunity to create a companion XIB file (and choose what device it will be if we make an iOS application).

enter image description here

When we click on further, we are not given a choice of what file name will have for our new file, but we still choose our location to save. However, when we actually create the file, we have a completely different set of code labels. What exact template code that we get completely depends on what is our base class, but regardless, the skeleton for our class is always in place:

 // // MyViewController.swift // Swift_iOS // // Created by Nick Griffith on 4/18/15. // Copyright (c) 2015 nhg. All rights reserved. // import UIKit class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */ } 
+20
source

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


All Articles