0% found this document useful (0 votes)
7 views2 pages

Chapter Tuple

The document discusses the use of tuples in Swift programming, highlighting their ability to group multiple values of different types into a single compound value. It provides examples of tuple comparisons, named tuples, tuple unpacking, and their application in functions and value swapping. Additionally, it demonstrates how tuples can be utilized in switch statements.

Uploaded by

zvipin.ahlawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Chapter Tuple

The document discusses the use of tuples in Swift programming, highlighting their ability to group multiple values of different types into a single compound value. It provides examples of tuple comparisons, named tuples, tuple unpacking, and their application in functions and value swapping. Additionally, it demonstrates how tuples can be utilized in switch statements.

Uploaded by

zvipin.ahlawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Apple introduced the stack view back in iOS 9 and made a bold claim for it during

a session on Auto Layout at WWDC 2015

//Custom Spacing (iOS 11)

//ios 14 background color

import Foundation

class ChapterTuple {

func callPrintFunctions(){
tupleComparison()
}

func tupleComparison(){

if(1,"zebra") < (2, "apple"){


print("True first")//this will true because 1 is less 2
}
if(1,"apple") < (1, "zebra"){
print("True Second")//this will true because a is less than z
}
if(1, "apple") == (1 , "apple"){
print("True Third")//this will true because both are equal
}
if("apple" , 1) < ("zebra" ,2){ //this a is less than z
print("True fourth")
}
if(2,"zebra") < (1, "apple"){
print("True Fifth")
}
else{
print("false")
}
}

func useOfTupleMeans(){
// Tuples group multiple values into a single compound value. The values
within a tuple can be of any type and do not have to be of the same type as each
other.
let tuple = ("one", 2, "three")
print("first value of tuple = \(tuple.0)")
print("second value of tuple = \(tuple.1)")
print("third value of tuple = \(tuple.2)")

// Also individual values can be named when the tuple is defined:


let namedTuple = (first : 1, middle : "dos", last : 3)
print("first value of tuple = \(namedTuple.first)")
print("middle value of tuple = \(namedTuple.middle)")
print("last value of tuple = \(namedTuple.last)")

// They can also be named when being used as a variable and even have the
ability to have optional values inside:
var numbers: (optionalFirst: Int?, middle: String, last: Int)
numbers = (nil, "dos", 3)
print("number tuple = \(numbers)")
}
func useOfGetFromTuple(){
let myTuple = (name : "Naminder kaur", age : 40)
let (first, second) = myTuple
print("first = \(first)")
print("second = \(second)")
// Specific properties can be ignored by using underscore (_):
let longTuple = ("abc", 123, "xyz")
let (_,_,third) = longTuple
print("third = \(third)")
}
func useOfTupleAsReturnFunc(){
let myTuple = self.tupleReturner()
print("First Value = \(myTuple.0)")
print("Second Value = \(myTuple.1)")
}
func tupleReturner()->(Int, String){
return (100, "Hundred")
}
func useOfTupleForSwaping(){
// Tuples are useful to swap values between 2 (or more) variables without
using temporary variables.
var a = "Naminder"
var b = "Samyra"
(b,a) = (a,b)
print("a = \(a)")
print("b = \(b)")
}
func useOfTupleForSwitch(){
let switchTuple = (First: true, Second: false)
switch switchTuple {
case (true, true):
print("true, true")
case (true, false):
print("true, false")
case (false, true):
print("false, true")
case (false, false):
print("false, false")
}
}
}

You might also like