Skip to main content

Swift Classes

In object oriented programming class is provides certain rules to implement class properties. Programmer implement new features and functionality by using of class rules. That means programmer are define a blue print structure to class.

And class mechanism are grouping data and function which are define by program inside a class. Object of class is capable to use this data outside of class or inherit class are capable to use to this method and data.

When we defining a class, that means we defining a new Data type in Swift programming. Class is a main concept of object oriented programming. In swift programming is define a class by class keyword.

class MyClass{
  //Data members
  //Member functions
}

Data member is related to data field of class, and member function is related to class member. Class are very flexible structure, we can define of many set of operation. For example.

class MyDetails{
  //Data members
   var name:String
   var age:Int

  //Member functions
  init(_ name:String,_ age:Int){
    //set initial value
    self.name=name
    self.age=age
  }
  //display data member values
  func details(){
    print("Hello I am \(name) and my age is \(age)")
  }
}
//make a object of class obj
var obj=MyDetails("Foo",11) //executed init block automatically
obj.details() //call member function to class
Make Class Object in Swift

Output

Hello I am Foo and my age is 11

Instance of class is accesses the class properties. That is also know as object of class. In above example create a object of MyDetails is obj.

var obj=MyDetails("Foo",11)

Create an object of class using of class name and parentheses. When need to set initial value of class data members. Then define init methods that accept this parameters values. That is similar to constructors of other object oriented programming language. Init methods are auto execute when create a respective object (parameter based).

self are commonly used when there are same as function parameter list and class data member name.

Deinitialization

Swift programming is based on Automatic Reference Counting (ARC). When creating a new instance of class and this instance referenced are assigned to any particular variable. In this situation this variable is capable to access value of class instance variable.

When of case this variable are defined inside particular scope (like inside a method). And this scopes are end then variable scope are also end. In this situation frees this instance memory which are store by variable.

class Operation{
    var type : String
    init(_ type:String){
        self.type=type
      
    }
    deinit {
        print("\(type) Operation Are Over ")
    }
    func test(){
      print("Start \(type) Operation")
    }
}
func main(){
  let heart = Operation("Heart")
  heart.test()
  //end of method and frees memory of instance variable
}

main()
Start Heart Operation
Heart Operation Are Over

In this example automatic memory managed by compiler. We can manually free allocated memory of instance in this way.

class Operation{
    var type : String
    init(_ type:String){
        self.type=type
        print("Start \(type) Operation")
    }
    deinit {
        print("\(type) Operation Are Over ")
    }
}

var heart:Operation?=Operation("Heart")

var cancer:Operation?=Operation("Cancer")

heart=nil //deallocation

Output

Start Heart Operation
Start Cancer Operation
Heart Operation Are Over 

Object referencing

class Reference{
    //Define member of class
    var x:Float
    var y:Float
    //set initial value to data member
    init(_ x:Float,_ y:Float){

        self.x=x
        self.y=y
    }
    func display(){
        print("x: \(x)")
        print("y: \(y)")
    }
}

var obj=Reference(10.0,20.0)
print("Before")
obj.display()

var temp=obj //assign reference to temp

temp.x=30.90 //update value

print("After")
obj.display()
Object Referencing

Output

Before
x: 10.0
y: 20.0
After
x: 30.9
y: 20.0

Copy object

In Swift Programming objects are have Reference type. Each object are capable to hold the reference of class instance. In case when assign one object to any other object. Then both objects are workers of same class instance variables. You'll can be view of this in above example. In this section you'll can view how to copy one instance variable values to another new instance.

//Copy data member 
class Reference{
    //Define member of class
    var x:Float
    var y:Float
    //set initial value to data member
    required init(_ x:Float,_ y:Float){

        self.x=x
        self.y=y
    }
    func copy()->Self{
        return type(of: self).init(self.x, self.y )
    }
    func display(){
        print("x: \(x)")
        print("y: \(y)")
    }
}

var obj=Reference(10.0,20.0)
print("Before obj")
obj.display()

var temp = obj.copy()

temp.x=30.90 //update value

print("After obj")
obj.display()

print("temp")
temp.display()
Copy Instances of Object

Output

Before obj
x: 10.0
y: 20.0
After obj
x: 10.0
y: 20.0
temp
x: 30.9
y: 20.0




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment