Swift Subscript With All The Way
Photo: Internet

Swift Subscript With All The Way

**Make Shortcut and Access Without hesitation**

Introduction :

When I first Start Learning Swift by Reading Apple swift book , I was become Freak of this freaky word Subscript . I used to Retrieve data from array by third bracket . But i notice some struct have a word subscript and those struct also Retrieve data by using third bracket ..so Today i will try to explain what is subscript and how it work magically .

Problem :

  1. How to Access list from the Struct, Class and Enum without any method

What is Subscripts ?

Subscripts is Nothing more than create a Shortcut for accessing the member elements of a collection, list, or sequence.In other word we can say subscript is the tool or No Name function for accessing elements from a struct ,class and enum .

Magical Subscript Syntax:

  1. In class or enum subscript syntax like a method just without method name.Its have parameter and return type like normal function with subscript keyword
subscript(parametername:Type)->returntype {
      get {
           //Return Subscripts Value Here 
       } set(newvalue) {
        //set the value 
        }
      }
 
  

Basic Subscripts Example :

lets Create a Album Struct which have a Array called song which contain all six song and have a subscript .

struct Album {
    var song = ["odhikar","bikhon","neel","jiboner utshobe","maya"]
     subscript(index:Int)-> String {
             return song[index]
         }
}

Accessing value by using Third bracket [ ]:

Now If We Want to Access song array we need to do code like this

let ablum = Album()
album[1] // bikhon

Here We Create instance of Album Struct and access 1 index value of song list by subscript.And Result should be “bikhon” become of This Album Struct have a subscript which return string value by its given index number .

Here is we used third bracket .This third [] bracket is not same as array accessing [ ]

If We Want Access song array in general way we need to do code like this :

let album = Album()
album.song[1]

hope It’s make Sense !!!

Subscript in Class :

In Class also its syntax are same as struct .Create class and its property and create no nome function with keyword subscript and return back its value

class Planet {
    var list:[String]
    init(list:[String]) {
       self.list = list
       }
       subscript(index:Int)->String {
       return list[index]
       }
}

Accessing Value From Class :

create instance of Planet with and array listOfPlanet and access planet list by [indexNumber]

let listOfPlanet = ["The Sun","Venus","Earth","Mars","mercury"]
let planet = Planet(list:listOfPlanet)
planet[2] // Earth

Calculation by Subscript :

This Struct have a store property called valueWithMultiply and its subscript take an Int value . In the Subscript body this store property valueWithMultiplymultiplied by taken Int value and return back and Int .

struct Multiplier {
       let valueWithMultiply:Int
       subscript(multiplyedBy:Int)->Int {
        return self.valueWithMultiply * multiplyedBy
      }
}

Use Multiplier :

So Create and instance of Multiplier with value 5 and multiplay by 5 . Result Should be 25 .

let fiveTimes = Multiplier(valueWithMultiply: 5)
fiveTimes[5] // 25 

Why Result 25 :

1.The Multiplier Struct have a subscript which take a Int value by its parameter(multiplyedBy).

2.In the subscript body Return a calculated value with that stored property (valueWithMultiply)and Parameter (multiplyedBy)

3.So Here When We Create instance “fiveTimes” with value 5 The store property (valueWithMultiply) get 5 .

4.Than when We access This instance by [5] its subscript parameter take 5 and calculate it and return back 25 …

For Today is Done Tomorrow I will Explain With Subscripts With Generic ..Stay Rock Still Tomorrow Thank you So much For Inspiring me by Stying with me ..

Conclusion : I Think Swift Subscript made Our Life Easy and give us ability to do code more Precise way ..

#swift #subscript #iOS





To view or add a comment, sign in

More articles by Md. Akramul Hoque

  • What Every iOS Develop Should know !

    Introduction: To become a iOS Developer we need clear concept with some topics first .Today I m listing down those…

    3 Comments
  • Access Control in :Swift

    Introduction : In this Article we will discuss and implement t Access control in swift. Access control is so important…

  • Completion Handler : Swift

    Introduction: When i was started learn swift i was pretty much confuse about some concept completion handler is one of…

  • Closure In Swift Part I

    How to Create function without name !! Introduction: Closure is one of the most useful future of swift.In this Three…

  • Beauty of Tuple in Swift

    What is Tuple in Swift : Tuple is a compound type in swift which can hold multiple type .And Very Simply tuple is a…

  • All About Array In Swift

    Introduction : In iOS Development we use Array Every day life .There is many way we can create array .

  • Grand Central Dispatch (GCD) Part II

    In Part One We cover serial and concurrent queue and example .On this part we will cover apple more about concurrent…

  • Grand Central Dispatch Queue (Part I)

    We all Know The Stupid computer can’t do more than one task at a time without concurrent programming or multiple CPU…

  • Swift Subscript With Generic

    So In This Part I will Explain bit more about subscript With Example .Here is part 1 if you miss that … Subscript in…

    2 Comments
  • iOS View Controller Life Cycle

    Introduction: For the iOS Apps Development Most Important Thing is to understand the Apps Life cycle and View life…

Explore content categories