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.
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.
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).
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.
}
*/
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With