DEV Community

Oluwasanmi Aderibigbe
Oluwasanmi Aderibigbe

Posted on

Day 11 of 100 days of swift ui

I just finished day 11 of hacking with swift ui. Today I learnt about Protocols and extension methods in Swift.

Protocols in Swift are a way of defining properties and methods a class or struct should have. The are created with the keyword protocol

Extension methods are a pretty cool feature in Swift that allows us to add methods to existing structs or class without modifying the struct or class

extension Int {
    func squared() -> Int {
        return self * self
    }
}
let number = 8
number.squared()
Enter fullscreen mode Exit fullscreen mode

The code above creates an extension method that squares an Int

Top comments (0)