Swift4 Subscripts

In Swift4, subscripts are the shortcuts which are used to access elements of a list, sequence or a collection. Subscript is used to set or retrieve a value using index instead of writing functions.

For example:

Subscript can be a single or multiple type declaration. It also ranges from single dimension to multiple dimensions according to the user's requirement for their input data type declaration.

Syntax:

The syntax of subscripts is same as computed properties. For querying type instances, subscripts are written inside a square bracket followed with the instance name.

Example:

Output:

The number is divisible by 50 times
The number is divisible by 33 times
The number is divisible by 25 times
The number is divisible by 20 times
The number is divisible by 16 times

Example2:

Output:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Subscript Overloading

In Swift4, subscript can take single to multiple input parameters belong to any data type. Defining multiple subscripts is known as subscript overloading where a class or structure can provide multiple subscript definitions.






Latest Courses