Search This Blog

Sunday, October 8, 2017

Test questions for iOS and Swift. Part 1


Keywords questions


1. Final Keyword

When using with class it means that class cannot be subclassed.
When using with method it mean this method cannot be overridden in subclass.

When try to subclass final class
import UIKit
import Foundation

final class Car {
    
    func drive() {
        print("Let's go!")
    }
}

class MyCar: Car {
    
    override func drive() {
        print("Let's go faster!")
    }
}

let lady = MyCar()
lady.drive()

This code does not compile.



When try to override final method
import UIKit
import Foundation

class Car {
    
    final func drive() {
        print("Let's go!")
    }
}

class MyCar: Car {
    
    override func drive() {
        print("Let's go faster!")
    }
}

let lady = MyCar()
lady.drive()


This code compiles fine.
import UIKit
import Foundation

class Car {
    
    func drive() {
        print("Let's go!")
    }
}

class MyCar: Car {
    
    override func drive() {
        print("Let's go faster!")
    }
}

let lady = MyCar()
lady.drive()
// print("Let's go faster!")

2. Fallthrough Keyword

In swift when using switch operator we don't need provide break in each case statements. So when case value that equal to search value is found then execution of switch if finished. If you need after execution of case statement go to the next case then you should use fallthrough keyword. There is one important thing about fallthrough - it doesn't check condition of next case it just go to the next case body or to default.
let value = 22

switch value {
case 22:
    print("This is 22")
    fallthrough
case 33:
    print("This is 33")
case 7658:
    print("This is 7658")
default:
    print("This is default")
}

// Result of switch:
// This is 22
// This is 33


3. Mutating Keyword

For example we have a struct (like below) with method which update some property(ies) of this struct.
import Foundation

struct Vehicle {
    
    var maxSpeed: Double
    
    func changeByFactor(factor: Double) {
        self.maxSpeed *= factor
    }
}

But this code will be not compiled. There is an error. The reason of this error is following: struct is value type in swift. This means properties of a value type cannot be modified from its instance methods.


So, for make code compilation we need add mutating keyword to function that change property of struct.
import Foundation

struct Vehicle {
    
    var maxSpeed: Double
    
    mutating func changeByFactor(factor: Double) {
        self.maxSpeed *= factor
    }
}

Lets create instance of this struct and assign it to constant.
let myVehicle = Vehicle(maxSpeed: 280.0)
myVehicle.changeByFactor(factor: 1.15)

And we got error here. This struct has mutating property so it must be var NOT let


This code is compiled fine.
var myVehicle = Vehicle(maxSpeed: 280.0)
myVehicle.changeByFactor(factor: 1.15)

4. inout keyword and symbol &

Inout keyword in function means that parameter that passed in function will be changed when local variable in function is changed. Without inout keyword passed parameters will remain the same after local modification. So, for example assume following code snippet:

This code does not compile.
func increaseValue(value: inout Double) {
    value *= 10
}

var value = 15.0
print("Old variable value is \(value)")
// Old variable value is 15.0

increaseValue(value: value)
print("New variable value is \(value)")
// Old variable value is 15.0


This code will not compile because we forget to insert & symbol before sending inout parameter.

Instead of:
increaseValue(value: value)

We must add & symbol before parameter:
increaseValue(value: &value)

This code compiles fine.
func increaseValue(value: inout Double) {
    value *= 10
}

var value = 15.0
print("Old variable value is \(value)")
// Old variable value is 15.0

increaseValue(value: &value)
print("New variable value is \(value)")
// Old variable value is 15.0

Syntax questions


1. Tuple destructuring

With special syntax we can get out values from tuple. This called "Tuple Destructuring". Main rule is count of "getting out items" must be equal to count of tuple members.
import Foundation

func getTuple(name: String, age: Int, specialisation: String) -> (String, Int, String) {
    return (name, age, specialisation)
}

let (first, second, third) = getTuple(name: "Aleksei", age: 28, specialisation: "Information Technology")
print("name is \(first), age is \(second) and specialisation is \(third)")
// name is Aleksei, age is 28 and specialisation is Information Technology

Operators questions


1. nil coalescing operator or "??"

With special operator "??" we can check value for nil and 
  1. if it is NOT nil then use this value
  2. if it is nil then use value after ?? operator
In example below dictionary does not have value for key "Steve", so value will be nil and we assign age constant to value -1.
import Foundation

let dict = ["Aleksei": 28, "John": 33, "Harry": 45]

let age = dict["Steve"] ?? -1
// age = -1 because dict does not have key "Steve"

This equals to this
let age: Int

if dict["Steve"] != nil {
    age = dict["Steve"]!
} else {
    age = -1
}

And equals to this
let age: Int

if let newAge = dict["Steve"] {
    age = newAge
} else {
    age = -1
}

And also we can write it with ternary operator
let age = (dict["Harry"] != nil) ? dict["Harry"]! : -1

If value is not equal to nil then operator ?? assign it to age constant.
let age = dict["Aleksei"] ?? -1
// age = 28

5 comments:

  1. It was not an easy topic to elaborate in such a short span of time. Custom Website is such a complex topic but you made it easy to understand

    ReplyDelete
  2. You've provided some really useful information; I've been looking for material like this, so please keep sharing it as much as you can. Best Custom Websites

    ReplyDelete
  3. Nice content, appreciable. It is such a kind of reading material that I was looking for to read.
    Uniqueness is the key to this content. Keep on providing me with such a great article.
    Create Custom Website

    ReplyDelete
  4. Thank you very much for sharing this informational blog.I could not find such kind of information in any other site.I'll come back for more fantastic material.
    Best wishes, and good luck.
    Custom Website

    ReplyDelete
  5. your article is unbelievable with accurate information. I see a lot of research after that and that is more than I expectedCustom Build Website

    ReplyDelete