Swift program to create a class and object


This tutorial will discuss how to write swift program to create a class and object.

Swift language is also an object oriented language. So it fully support class and objects.

Class

A class is a blueprint for the objects. To add the functionality to the class we can define properties and methods. Here, the constants and variables present inside the class are known as the class property whereas a function present inside a class is known as a method. Just like other programming language, Swift classes also support inheritance, type casting, deinitializers, reference, etc.

Syntax

Following is the syntax to create class −

Class MyClassName{
   // Definition of the class
}

Example

The following program shows how to create a class.

class Course{
   var name = “Swift”
   var chapters = 40
   func Price(){
      print(“Price of swift package is 5000”)
   }
}

Here, Course is the class name, name and chapters are the properties of the class, and. Price is the method of the class. We can access the class properties and methods with the help of dot syntax. Here, the property name is separated by a dot(.) after the instance name for example, obj.name.

Object

In Swift, a class object is known as instance of a class. We are allowed to create multiple objects of the same class. For example, book is a class and author1, author2, and author3 are the objects of the class.

Syntax

Following is the syntax to create object −

var MyObjectName = MyClassName()

Example

The following program shows how to create an object −

// Creating class
class Course{
   var name = “Swift”
   var chapters = 40
   func Price(){
      print(“Price of swift package is 5000”)
   }
}

// Creating object
var lang1 = Course()

Example

Class and object

The following program shows how to create a class and its object.

import Foundation
import Glibc

// Creating class
class Author{
   // Properties
   var language = "Swift"
   var chapters = 50
   var programs = 60
   // Methods
   func price(){
      print("Cost of one package is 6000")
   }
}
// Creating object 
var lang1 = Author()
// Accessing the properties of the class
print("Language name: ", lang1.language)
print("Total number of chapters:", lang1.chapters)
// Accessing the methods of the class
lang1.price()

Output

Language name:  Swift
Total number of chapters: 50
Cost of one package is 6000

Example

Creating multiple objects of the class

The following program shows how to create multiple objects of a class.

import Foundation
import Glibc

// Creating class
class Product{

    // Properties
    var productID = 0
    var ProductName = "Mango"
}

// Creating multiple objects
var obj1 = Product()
var obj2 = Product()

// Accessing the properties of the class
obj1.productID = 3456
print("Id of the product:", obj1.productID)

obj2.productID = 876
print("Id of the product:", obj2.productID)

Output

Id of the product: 3456
Id of the product: 876

Updated on: 13-Dec-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements