Swift - Programming Language


Classes

Class is a specification to capture attributes or properties and behaviour of a specific type of objects in a categorised manner. Say Human is a type of specimen and it has some properties like number of legs, number of eyes, height, weight, hair colour etc etc. Also it has some behaviour like it can walk, it can run, it eats, it sleeps, it speaks etc etc. So when we have to draw a guidelines to say a specific object belongs to Human class then we categorically list down all the behaviour and properties which we can identify for surely say that this object it human. This listed down properties and behaviours is known as class. A Class is basic building block of swift programming.

//Define a class
class MyFirstClass {

// variables and constants give below are known as properties
var name:String = ""

// functions given below are known as methods in class
func saveName(var name:String){
self.name = name
}

func printName(){
print(self.name)
}
}

//Use a class - create an object of class type MyFirstClass
let myClass = MyFirstClass()
myClass.saveName("Pawan")
myClass.printName()

Struct

Structures in swift are similar to class in many ways and it is also a collection of properties (variables) and methods (functions).

struct Rectangle {
var width:Int32 = 0
var height:Int32 = 0

mutating func dimensions( width: Int32, height: Int32){
self.width = width
self.height = height
}

func area() -> Int32
{
return self.width * self.height
}
}
//Create an object of struct type Rectangle.
var rectangle = Rectangle()
rectangle.dimensions(20, height: 30)
rectangle.area()

Similarities in Class and Struct

  1. Both can store values and have functionalities defined.

  2. Both can set-up initialiser methods and initialise values.

  3. Both can adopt protocols.

Differences in Class and Struct

  1. Structure can't inherit from other Structure but Class can.

  2. In runtime, a class object could be checked to find it's Class.

  3. Deinitializers enables a class to free up memory for it's resources.

Revision and Practice:

  1. Write a class to define a student which has following properties -

    • Roll Number [Unsigned Integer]
    • Name [String]
    • class / standard [integer]
    • subjects - [This would be an array]
  2. Write a structure to define a student which has following properties

    • Roll Number [Unsigned Integer]
    • Name [String]
    • class / standard [integer]
    • subjects - [This would be an array]
  3. Enhance the class and structure both to store subjects and their scores obtained in subject. Assume total mark as 100 and pass mark as 40.

  4. Write a method in both class and struct to calculate the grade obtained by a student. Formula to find grade is below

    • Grade A+ : 90 - 100

    • Grade A : 80 - 89

    • Grade B : 70 - 79

    • Grade C : 60 - 69

    • Grade D : 50 - 59

    • Grade E : 40 - 49

    • Grade F : 0 - 39


References:

  1. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html