Extensions in SwiftUI allow you to add functionality to existing types without modifying their original source code. This means you can enhance built-in types, your own custom views, and even types from external libraries.

Key points:

In my words, the string type has already built in functions when we add a fullstop for eg, lowercase(), uppercase() etc. Now if we make a funtion it isn’t grouped in the string part thus difficult to find and overall the code get’s cluttered, plus it won’t work. That is why we use extension which adds functionality to existing types.

extension String {
    
    func removeWhitespace() -> String {
        return components(separatedBy: .whitespaces).joined()
    }
}

let alphabet = "A B C D E F"
print(alphabet.removeWhitespace())

output

ABCDEF