What is Tuple in Swift



What is Tuple?

  • A tuple is a group of zero or more values represented as one value.
  • Tuples in Swift are like mini structs and should be used for storing temporary values only. They are commonly used to return multiple values from a function call.
  •  Tuple is value types. When you initialize a variable tuple with another one it will actually create a copy.
  • In swift we have two types of tuples are available those are unnamed tuples and named tuples.
  1.  Unnamed Tuples
  • In swift, unnamed tuples are used to group multiple values into single by using parentheses and those are need to be separated by commas. 

      let userData = ("Pawan Manjani", 200) 
 
        
       print(userData.0) //Pawan Manjani 
       print(userData.1) //200


     2.   Named Tuples
  • In swift, named tuples are used to group multiple values into single by using parentheses and need to define those values with named variables and those are separated by commas.

     let userData = (name: "Pawan Manjani", id: 200) 
 

     print(userData.name)   //Pawan Manjani 
    print(userData.id)      //200





Thanks for reading, Happy Coding πŸ‘¨‍πŸ’»

Popular posts from this blog

Frame vs bounds in Swift

DateFomatter in Swift

Property wrapper in Swift