iOS Development Swift 2 - Classes & Objects


Advertisements


Let us understand what Classes and Objects are in detail.

Classes

Classes are the general-purpose flexible constructs that are the building blocks of your program’s code. You can define properties and methods for your classes by using the same syntax as we did for variables and functions.

The syntax of defining a class is as follows.

class className { 
   var anyVariable = anyValue 
   // Function Definition etc. 
}

Objects

Object is the term that is generally used to refer to instance of a class, so we can call it instance instead of objects.

Example − Making a class and its object.

class Person { 
   var firstName = “Tutorials” 
   var lastName = “Point” 
   func printName() { 
      print(firstName + “ ” + lastName) 
   } 
}

Initializing an object − To initialize an object, we should use the following command.

let anyone = Person()  // We can initialize an object by default constructor. 

ios_development_with_swift2_playground.htm

Advertisements