Swift Program To Create A Custom Subscript

You are currently viewing Swift Program To Create A Custom Subscript

Swift Program To Create A Custom Subscript

// Swift program to create a simple subscript

import Swift

struct Sample {
    let value: Int
    subscript(index: Int) -> Int {
        return value * index
    }
}

let S = Sample(value: 5)

print(S[1])
print(S[2])
print(S[3])
print(S[4])
print(S[5])

Output

5
10
15
20
25

...Program finished with exit code 0
Press ENTER to exit console.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply