Question: Swift Optionals 1.let optvar : Int = nil Correct the error in this line of code. 2.let unwrapme : String? = nil let unwrappedValue :
Swift Optionals
1.let optvar : Int = nil Correct the error in this line of code.
2.let unwrapme : String? = nil
let unwrappedValue : String = unwrapme!
The code snippet shown above will crash. Rewrite it with Optional Binding.
1.Declare any optioanal variable of any type with the Optional keyword
var value1 : Int?
var defaultValue : Int = 7
Print the value of value1 to the console. If it contains nil use assign defaultValue to it.
i) Write a simple if-else block to do so.
ii) Use the nil coalescing operatior.
If let name = txtname.text {
If let address = txtaddress.text {
sendToServer(name , address) }
else {
print (No address provided)
} } else { print (No name provided) }
Rewrite this piece of code using 2 guard statements.
Swift Dictionary and Tuples
1.Create an array of dictionaries in which each dictionary in the array contains the keys firstName and lastName. Create an array with a name of your choosing that contains only the values for firstName in each dictionary.
2.Using the array of dictionaries created previously, this time create an array that contains the values for firstName and lastName in each dictionary separated by a delimiter of your choice.
3.Create a datatype called MyTuple using the typealias feature of swift. It should be a tuple containing 2 Strings (String , String).
4.Declare and initialize a tuple with any values of your choice. 5.Print both values of the tuple individually in the console.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
