Subscripts in swift

Subscripts are used to access information from a collection, sequence and a list in Classes, Structures and Enumerations without using a method. These subscripts are used to store and retrieve the values with the help of index without the use of separate method. To access elements via subscripts write one or more values between square brackets after the instance name.

For example: Array elements are accessed with the help of someArray[index] and its subsequent member elements in a Dictionary instance can be accessed as someDicitonary[key].

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.

Syntax: (This looks a lot like a Swift computed property.)

The parameter list can take multiple values. The getter method id required. The setter method is optional. The parameter newValue in the setter method is implicitly declared. You can change it to anything you want which will improve the readability of the code.


Here is another example from

it prints:


If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!



iOS and tvOS developer, dreamer, photographer 🤨

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