Swift Tuples

  Total Views : 341
  Comments : 0
  Keywords : Swift tuples to group multiple values together into single value with examples, Swift access tuple values with wildcard characters with examples, Swift tuple types (named & unnamed) examples

Here we will learn tuples in swift with examples and how to use swift tuples to test more than one value in same switch case statement with examples.

Swift Tuples

In swift tuples are used to group multiple values together into single compound value. The values whatever we will define in tuple can be of any type and there is no restriction to use same type of values. 

 

In swift we have two types of tuples are available those are unnamed tuples and named tuples.

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. 

 

Following is the simple example of defining unnamed tuples in swift programming language.

 

let userInfo = ("Suresh Dasari", 200)

 

Or

 

var userinfo:(String, Int) = ("Suresh Dasari", 200)

If you observe above unnamed tuple values we are grouping string and integer values into single compound value and we are using immutable let or mutable var based on our requirements.

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.

 

Following is the simple example of defining named tuples in swift programming language.

 

let userinfo = (name: "Suresh Dasari", id: 200)

 

Or

 

var userinfo:(name:String, id:Int) = ("Suresh Dasari", 200)

As we discussed in tuple we can use any type of values within the tuple and it’s restricted to use same type like arrays.

 

If you observe above tuple example we defined values with variable names and we are grouping String and Integer type values into single tuple value.

 

In above examples we created immutable or unchangeable tuple using let and mutable tuple by using var keyword based on our requirement.

 

Now we will see how to access these tuples values in our swift programming language with examples.

Swift Access Tuple Values

Following is the simple example of accessing unnamed and named tuple values based on our requirements in swift programming language.

 

// Constructing a simple tuple

let tp1 = ("Suresh Dasari", 200)

print(tp1.0)

print(tp1.1)

 

let tp2 = (5, 10, 7)

print(tp2)

 

let (uname,uid) = tp1

let result = uname +", "+ uid

print(result)

 

// underscore (_) to ignore value

let (onlyname,_) = tp1

print(onlyname)

 

// Constructing a named tuple

let un = (name: "Suresh Dasari", id: 200)

print(un.name)

print(un.id)

If you observe above tuple example we defined both named and unnamed tuple types and accessing those values based on our requirements.

 

When we execute above program in swift playground we will get result like as shown below.

 

Suresh Dasari

200

(5, 10, 7)

Suresh Dasari, 200

Suresh Dasari

200

If you observe above example we used underscore (_) character for wildcard pattern or to ignore the parameter value.

 

Now we will see how to use tuples in switch case statement with examples in swift programming language.

Swift Tuple with Switch Case Statement

Following is the simple example of using tuple with switch case statement in swift programming language.

 

let coordinates: (x: Int, y: Int, z: Int) = (3, 2, 5)

 

switch coordinates {

case (0, 0, 0):

print("Origin")

case (_, 2, 5):

print("On x axis.")

case (1, _, 0):

print("On y axis.")

case (1, 2, _):

print("On z axis.")

default:

print("Somewhere in space")

}

When we run above example we will get result like as shown below.

 

On x axis.

Following is another example of using tuple in swift programming language.

 

let coordinates: (x: Int, y: Int, z: Int) = (3, 2, 5)

switch (coordinates) {

case (0, 0, 0):

print("Origin")

case (let x, 0, 5):

print("On the x-axis")

case (3, let y, 5):

print("On the y-axis")

case (0, 0, let z):

print("On the z-axis")

case (let x, let y, let z):

print("Default Coordinates at x = \(x), y = \(y), z = \(z)")

}

When we run above program we will get result like as shown below.

 

On the y-axis

This is how we can use tuples in swift programming language to group multiple values into single compound value to use it in applications based on our requirements. 

 

NEXT

Swift For Loop  
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User