Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

cocoa

I have been learning Swift language for a while, I am stuck with the one of the basic question, i.e. what is the basic difference between the file templates of a Cocoa Touch Class and a normal Swift class? Can some one provide any basic difference along with example? I know they are different because they each have an option for creating a file.

like image 970
Keshav Kumar Avatar asked Apr 18 '15 19:04

Keshav Kumar


Video Answer


1 Answers

TL;DR: They're not different types of classes. They're just different file templates.


According to your comments:

I know they are different coz they are different options for creating a file, yet they are the same.

They aren't different options though.

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 choose Swift File, the next dialog screen asks us to give our file a name and choose a save location. When we click "Create" from here, we simply get an empty(ish) Swift file with the name we picked.

All that's dropped into the file is the boilerplate copyright information and an import Foundation line:

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

import Foundation

That's the entire file created.


If we choose Cocoa Touch Class, however, we get an entirely different dialog.

We are asked to give our class a name, choose what its base class is, and pick a language. Additionally, if our base class is some sort of view controller, we will be given the option of creating a companion XIB file (and choose what device it is for if we're doing an iOS app).

enter image description here

When we click next, we are not given a choice on what file name our new file will have, but we still get to pick our save location. Once we actually create the file, however, we have an entirely different boiler plate set of code. What exact boilerplate code we get entirely depends on what our base class is, 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.
    }
    */

}
like image 127
nhgrif Avatar answered Nov 16 '22 00:11

nhgrif