Subscripts In Swift


Subscripts are typically used as a shortcut for accessing the collection, sequence, and a list in Classes, Structures, and Enum without using a method. It used to store and retrieve the values without the use of separate methods. To access elements via this write one or more values between square brackets after the instance name. It can take any number of input parameters, and these input parameters can be of any type.

For a single type, subscripts can range from single to multiple declarations. We can use the appropriate subscript to overload the type of index value passed to the subscript.


Consider the Matrix structure, which represents a two-dimensional matrix of  Double values. It takes two integer parameters (rows and columns) :

Matrix is initialized by two parameters (rows and columns)  and creates an array of size rows * columns to store double value. Initially, we store value for each index in the array is 0.

So, let’s create the Matrix instance by adding the row and column in the parameter of its initializer.

Now, the grid array form as:

So, set the value of the Matrix by adding the row and column.

So, the grid array changed as:


The Matrix subscript’s getter and setter and both have checked the assertion that the subscript’s rows and columns values are valid. Because in some cases it can be out of the index.



So if you have any comments, questions, or recommendations, feel free to post them in the comment section below!

. . .

Leave a Comment

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

Be the first to comment.


All communication is done through a ticket system. I appreciate the regular almost daily updates of what is being worked on, and communicating changes or updates is easy through this system. Mobikul's response timeframe is quick.

Josh Arnold

Josh Arnold

Owner, Shop ATV Escape

Talk to Sales


Live Chat
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home