0Pricing
Swift Academy · Lesson

Subscripts

Subscripts

Subscripts is a free Swift Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Hello,

Subscripts are a shortcut to access member items of a collection, list, or directory.

Use subscripts to set and retrieve values ​​according to the index without the need for separate getter and setter methods.

// The use of arrays in literal form is an example of subscripts.

var swiftArray = [ "Apple" , "Google" , "Amazon" , "Microsoft" ] 
swiftArray [ 2 ] = "IBM" 
print(swiftArray)

//replaced Amazon with IBM at index 2. index in array start from 0.

cont.

Subscript can be defined for classes, structs, and enums.

Subscripts are used to access the elements of a collection-type object. We can use subscripts to get and get according to the index.

cont.

So we don't need extra methods.

For example, we can access the elements of an array with someArray [index] or the element of a dictionary with someDictionary [key].

We are making these uses all the time actually, these are subscripts written for Swift.

subscript type

We can define more than one subscript for a type.

The appropriate subscript will be selected according to the type of index that we have inserted in the subscript (it will be overloaded).

Subscripts are not limited to one dimension. Thus, we can define multiple input parameters specific to the needs of the type used for a subscript.

example

Subscripts allow us to query an instance (with arguments that we will insert in the brackets after the name).

// In this example instance in subscription is called
instance[1]

How to Write a Subscript?

How to Write a Subscript?

First of all, we write the subscript keyword. Then we define it as one or more parameters and write the return type.

This is the same method as we know the syntax. However, unlike the instance method, subscripts are read-write or read-only.

This behavior is passed by getter and setter to computed properties in the same way:

// subscript keyword has been defined
// one index was passed as parameter
subscript(index: Int) -> Int {
    get {
        // Return an appropriate subscript value here.
    }
 set(newValue) {
        // Perform a suitable setting action here.
    }
}

cont.

As with read-only computed properties, we can define a read-only subscript by removing the get keyword and parentheses.

Congratulations

Congratulations 🎉

In this lesson, we have learned Subscripts on Swift.

See you in the next lesson.

Subscripts — illustration 8

Frequently asked questions

Is the “Subscripts” lesson free?

Yes — the full text of “Subscripts” is free to read here on the web, and the Swift Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Subscripts”?

Subscripts You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Subscripts” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to Swift Academy