Swift Program To Demonstrate The ‘self’ Keyword Within The Class

You are currently viewing Swift Program To Demonstrate The ‘self’ Keyword Within The Class

Swift Program To Demonstrate The ‘self’ Keyword Within The Class

// Swift program to demonstrate the "self" keyword 
// within the class

import Swift

class Sample {
    var num1:Int
    var num2:Int

    init(num1:Int, num2:Int) {
        self.num1 = num1
        self.num2 = num2
    }
}

let obj = Sample(num1:10,num2:20)

print("Num1: ",obj.num1)
print("Num2: ",obj.num2)

Output

Num1:  10
Num2:  20

...Program finished with exit code 0
Press ENTER to exit console.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply