Lesson 13 - Overloading operators and other useful syntax in Swift

In the previous lesson, Type casting and object hierarchy in Swift, we talked about interfaces.

While the previous lessons may have been considered necessary for you to actually create something in Swift, this is rather a complementary lesson and you could even skip it. On the other hand, it's not long and knowing about these options might be useful for you.

Operator overloading

You have for sure encountered operators such as +, - or * in programming. We use them to perform numerical math operations. Under the hood, operators are special functions. They are defined for numbers, but we can easily define them for any of our custom type (i.e. structure or class). It's simply a method whose name is the given operator.

This may sometimes be useful. However, you can, of course, name the method as e.g. add() sum up two instances in it and return the result, avoiding using the + operator at all.

As an example, we can use a Point struct, which represents a point in the 2D space with x and y coordinates. The struct declaration looks as follows:

struct Point {
    var x: Int
    var y: Int

When frequently used, it would make sense to create an operator for adding two points. Even using this syntax will be more clear. Therefore, we'll declare the + operator to the point as its static method:


...End of the preview...

Premium article

Premium article

ict.social is a large database made up of manuals and tutorials, whose main goal is to provide high-quality IT education to everyone. We started out in the Czech republic, where we display roughly a million articles per month and receive plenty of gratitude from our users. Thanks to our successful establishment, we are now bringing these articles to the rest of the world.

Although we are trying to keep our content free of charge, maintaining the site is a huge effort for everyone involved. Therefore, some content (exercises and more advanced material) costs network points. Don't worry, they're really cheap :)

Article description

Requested article covers this content:

We'll show you how to define operators for custom types, including custom operators, and a few more useful things in Swift.

Limited offer: Learn all knowledge and save money

Buy articles and tests separately one by one 64 points
Buy all currently available articles in the section including all features for an exclusive price 54 points
Currently, you have 0 points
By buying this exclusive package, you'll have access to all 16 articles in this course including exercise submitting while saving $0.86. This offer is limited for the first articles only with an additional exclusive 15% discount.
You gain 54 points for adding an article to the site, or for $5.76 $4.90

Warning, by buying just this article you'll lose the limited 15% discount for the package of all the articles.

To access the article, you need 16 points
Currently, you have 0 points
You gain 16 points for adding an article to the site, or for $1.44

Buying this article gives you unlimited access to it forever. You will learn some more and help us keep giving our site maintenance which helps you and others get better futures. It's a win-win.

This article is licensed: Premium II, by buying this article, you agree with the terms of use.

You gain points by supporting our network. This is done by sending a helpful amount of money to support the site, or by creating content for the network.

You can get points immediately using:

Credit card SMS Wire transfer
Credit card SMS Wire transfer
Article has been written for you by Filip Němeček
Activities (4)