How to use Extension in iOS with swift 5

How to use Extension in iOS with swift 5
April 26, 2020 No Comments Development,iOS Development Pushpendra Kumar

In this tutorial, you will learn about what is the extension? What is the use of extension in iOS with a swift 5 programming language? And also you will learn that, How you can integrate the extensions in your iOS project in a very easy step. So watch the complete video and follow my YouTube channel for the upcoming videos.

Extension in iOS

An extension can extend an existing type to make it adopt one or more protocols. In Extension you can create hidden methods. Those methods could be very useful for your application. And same method you can use at multiple places. You want more deep clarification about the extension then you can watch my video on my YouTube channel.

So now, Let’s see practically about it. Firstly create one new swift file in your project with the name extension. Which is given in image.

How to use extension in ios
How to use extension in ios

After that, Write the following code in this file. And for your kind information, I would like to tell you that. In this example, we are going to use a simple extension example. Which just a multiplication of integer number.

extension Int {
    func cube() -> Int {
        return self * self * self
    }
    
    
    func hexa() -> Int {
        return self.cube() * self.cube()
    }
}

Nice😊😊😊😊 After that, Let see how to use this extension method. And just for information. In this method, we are multiplying integer numbers. Okay…! So now you can access this function everywhere in your project wherever you are using integer number. So let’s see practically this

    override func viewDidLoad() {
        super.viewDidLoad()
        let a : Int = 2
        print("CUBE = \(a.cube())")
        print("HEXA = \(a.hexa())")
    }

Great work 😎😎😊 Now Run ▢️ your project and you will see the below output.Β 

CUBE = 8

HEXA = 64

In Conclusion, I hope it is clear that what is the extension and how to use the extension in iOS development with a swift 5 programming language. But if still, you have any confusion for the integration then you can visit on my YouTube video. In My Youtube video, I have explained deeply about extension and its usage. And also do not forget to subscribe to my YouTube channel. If you are looking more interesting topic then follow the below links.

Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *