Friday, March 30, 2018

Swift 4 Introduction Series 1.5 - Swift Data Type Tuples

Tuples

Tuples is formed by grouping 2 or more basic data type into a single compounded data type. Each element of tuples can be of any Swift basic data type. The data type can be different among each element in the tuples. We can create tuples from any permutation of types since tuples can contain many different types. Tuples are not suitable for complex  and persistent data structure. For complex and persistent data structure we should use structure or object class.


Create and Declare Tuples

To create a constant tuple, we use the same keyword let and to create a variable tuple, we use the keyword var. The behavior of constant and variable is the same as Swift basic data type. Constants are immutable and variables are mutable.


We can form a tuple with a string and integer as follows:


let contact = ("My Name", 22341176)




To access the element in the tuple, we use index starting from 0.


Example:


contact.0
contact.1




As usual, we can also create a tuple by just declaring them first. We just need to include all the different data type in a brackets separating them using a comma.


Example:


let address1:(String, String)
address1.0 = "Apartment Block A"
address1.1 = "New Jersey"
address1.0
address1.1




Please note that although we can declared constant first without assigning data. This is a not best practice. For declaring constant, we should declare and assign them at the same statement. This is to make our code safer. We should only declared tuples and assign the data later when absolutely necessary. For variables, we should declared and initialized the variable first even if we do not have data to input.


The prefered method for creating tuple variable is to create and initialize the variable at the same time using the following method.


var contact1 = (String(), Int())
contact1.0
contact1.1
contact1.0 = "This is a test"
contact1.1 = 2132
contact1.0
contact1.1
contact1.0 = "David"
contact1.1 = 76788779
contact1.0
contact1.1




We can have same data type for each element in the tuple.


var address2 = (Int(), Int(), Int())
address2.0
address2.1
address2.2

address2.0 = 12
address2.1 = 1231
address2.2 = 987
address2.0
address2.1
address2.2

address2.0 = 1
address2.1 = 243
address2.2 = 77234
address2.0
address2.1
address2.2




Syntax to Create Tuple

The syntax for creating tuple


let/var <tuple_name> = (<first_element_value>,...<last_element_value>)
OR
var <tuple_name> = (<first_element_datatype>(),...<last_element_datatype>())


To create and declare tuples that uses data type that cannot be inferred. Use the syntax below:


let/var <tuple_name>:(<first_element_datatype>,...,<last_element_datatype>) =
(<first_element_value>,...,<last_element_value>)


Construct and Deconstruct Tuple

To construct a tuple using multiple variable, we use the syntax of creating tuple but we substitute the value with variables.


Example:


let originalContactName = "Thomson"
let originalContactNumber = 8273294
let newContactTuple = (originalContactName, originalContactNumber)
newContactTuple.0
newContactTuple.1




To construct a tuple using non standard data type


let element1:Int8 = 87
let element2:Character = "a"

let someTuples2 = (element1, element2)



More Example:

let someNonStdTup:(UInt,Float,Int16,String) = (88, 3.14, 32, "mixed of std and non std data") someNonStdTup




To deconstruct a tuple into multiple variables we use the syntax below:


let/var (<variable_element1>,...,<variable_elementLast>) = <tuple_name>


See example below:


let contact5 = ("Steve", 81729382)
contact5.0
contact5.1

let (contactName, contactNumber) = contact5
contactName
contactNumber




While deconstructing a tuple, we can use _ if we do not want the element.


Example:


let someTuple1 = (123,1324,323,545)
let (getfirstNumber, _, _, _) = someTuple1
getfirstNumber

let (_, _, getThirdNumber, _) = someTuple1
getThirdNumber

let (_, getSecondNumber, _, getLastNumber) = someTuple1
getSecondNumber
getLastNumber




Create Tuples with Element Labeled

We can also create tuples and labeled each element in the tuples. This helps to identify or refer to the element easily instead of using index number.


The syntax is as follows:


let/var <tuple_name> = (<element_name>:<first_element_value>,...,<element_name>:<last_element_value>)
OR
var <tuple_name> = (<element_name>:<first_element_datatype>(),...,<element_name>:<last_element_datatype>())


By creating tuple with label, we can always refer to tuples' element using the labeled name instead of index.


Example:


let contact7 = (name:"Alan", number:18273949)
contact7.name
contact7.number

var contact8 = (name:String(),number:Int())
contact8.name
contact8.number
contact8.name = "Jimmy"
contact8.number = 82736422
contact8.name
contact8.number




The example below summarized different way to create and declare tuples:


// Comprehensive Tuples creation
// Tuples creation with inference
let contactBook1 = ("name",2837993)
contactBook1.0
contactBook1.1

var contactBook2 = ("second name",823874)
contactBook2.0
contactBook2.1

// Tuples creation without data
var contactBook3 = (String(),Int())
contactBook3.0
contactBook3.1

// For constant, NOT ENCOURAGE, use it when absolutely necessary
let notEncourageContactBook4:(String,Int)
notEncourageContactBook4.0 = "some name"
notEncourageContactBook4.1 = 827374623

// Tuples creation with label
var contactBook5 = (name:String(),phone:Int())
contactBook5.name
contactBook5.phone
contactBook5.name = "some other name"
contactBook5.phone = 87232456

// Tuples creation with non-standard data types, no data type inference

let someTuples1:(Int16, Float) = (12, 3.4)
someTuples1.0
someTuples1.1

let secretContactBook1:(Character,Int16) = ("A",877)
secretContactBook1.0
secretContactBook1.1

var secretContactBook2:(secret:Character, code:Int16) = (" ", Int16())
secretContactBook2.secret
secretContactBook2.code
secretContactBook2.secret = "q"
secretContactBook2.code = 236
// Note data type character cannot be initialized using Character()






Using Tuples

We can pass the values of tuples to new variable or constant like other basic data type.


Example


let someTuple2 = (name:"Richard", number:298323, socialSecurity: 834783748374, address:"123 street Apartment A")
var someTuple3 = someTuple2

someTuple3.number
someTuple3.number = 87777767

var someTuple5 = (name:"", number:0, socialSecurity: 64, address:"A")
someTuple5 = someTuple3




If we are to pass value from a variable to another, we must make sure the data type in both variables matches and in the same order.


Example:


let someT1 = (12,54)
someT1.0
someT1.1

var someT2 = (0,0)
someT2.0
someT2.1

someT2 = someT1
someT2.0
someT2.1




Another example:


var someT3 = (123, "test", 183, 823.0)
var someT4 = (0, "", 0, 0.0)
someT4 = someT3
someT4.0
someT4.1
someT4.2
someT4.3




We can assign or change a single element as show below:


// The following example is continuation of previous example
let tempDouble1 = 283.9
someT4.3
someT4.3 = tempDouble1
someT4.3




If we are labeling the tuple element, then to pass value from one variable to another, not only the data type must match, the labeled must also be the same.


Let's consider the following example:


let someTuple11 = (a:87, b: 897)
var someTuple12 = (s1:0, s2:0)
var someTuple13 = someTuple11
someTuple13.a
someTuple13.b




Variables someTuple12 and someTuple13 have the same data type but each variable are named different. If we are going to assign someTuple13 to someTuple12, the system will generate error as shown below:




However, we can pass the value to each other element wise:


// someTuple12 = someTuple13
// We can however pass the value element wise
someTuple12.s1 = someTuple13.a
someTuple12.s2 = someTuple13.b




If we are going to create another variable that matches the data type and name, then we can pass the values by pass the tuple to another


// Continuation from previous code
var someTuple14 = (a:0, b:0)
someTuple14 = someTuple13
someTuple14.a
someTuple14.b


Another example:


let someTuple20 = (name:"Richard", number:298323, socialSecurity: 834783748374, address:"123 street Apartment A")
var someTuple21 = someTuple20

someTuple21.number
someTuple21.number = 87777767

var someTuple22 = (name:"", number:0, socialSecurity: 64, address:"A")
someTuple22 = someTuple21




Application of Tuples

  • Tuples is design as a temporary holder to hold a bunch of related values.
  • For C programmer, although tuples is similar to struct but it is intended to use as a temporary values holder. Swift also support struct which is more powerful.
  • Tuples is useful when we want to pass a function with multiple return to a variables.


Example of using function with multiples return


func getMultiData() -> (Int, String) {
   
   let a = 123
   let b = "someTest"
   return (a,b)
}

let getData = getMultiData()
getData.0
getData.1



***

No comments:

Post a Comment