0Pricing
Swift Academy · Lesson

Subscripts

Subscripts

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.

← Back to Swift Academy