Custom Operators in Swift

Gurjit Singh
May 4, 2020 · 2 min read
Photo by Christine Donaldson on Unsplash

Swift provides a very useful feature which is to define your own custom operators. It is a very confusing topic in the beginning but it is very powerful to use. It increases our code readability and provides a very clear and concise way to write code.

You can declare custom operators by defining static type method inside an extension. First of all, you have to decide your operator type that is it prefix, infix or postfix.

Prefix Operators

extension String {
static prefix func !! (name: String) -> String {
return "Hello " + name
}
}
print(!!"Gurjit")
//will print Hello Gurjit

In the above example:

  • First of all, implements extension String
  • Then, defines prefix operator !! and this method started with the static keyword
  • The static method takes one string argument called name
  • Finally, return string after concatenating name argument with “Hello”

Infix Operators

We are not talking about precedence and associativity in this article but if you want an explanation for this topic please go-ahead to this link.

infix operator ^^: MultiplicationPrecedenceextension Int {
static func ^^ (num: Int, power: Int) -> Int {
return Int(pow(Double(num), Double(power)))
}
}
print(5 ^^ 2)

In the above example,

  • First, defines a custom infix operator ^^ which belongs to MultiplicationPrecedence group
  • Then, implements extension Int
  • Defines static function ^^ which takes two parameters num and power and return the power of the number

Postfix Operators

extension Double {
static postfix func % (percentage: Double) -> Double {
return (Double(percentage) / 100)
}
}
print(30%)
//will print 0.3

In the above example:

  • First of all, implements extension Double
  • Then, defines postfix operator % and this method started with the static keyword
  • The static method takes one string argument called percentage
  • Finally, return double after calculating the percentage

Conclusion

It’s very handy to write custom operators to write robust code when working in teams, that code is understood by others.

Thanks!

Mac O’Clock

The best stories for Apple owners and enthusiasts

Gurjit Singh

Written by

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me on twitter @gurjitpt and for more articles www.gurjit.co

Mac O’Clock

The best stories for Apple owners and enthusiasts

Gurjit Singh

Written by

I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me on twitter @gurjitpt and for more articles www.gurjit.co

Mac O’Clock

The best stories for Apple owners and enthusiasts

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store