- Defer statements cannot execute any statement which will transfer control outside like return, break, throw.
- Defer statements are executed in reverse order i.e. last defer is executed first
Example from apple docs is
func processFile(filename: String) throws { if exists(filename) { let file = open(filename) defer { close(file) } while let line = try file.readline() { // Work with the file. } // close(file) is called here, at the end of the scope. } }
defer is used to call close() at the end of processFile function.
Question 17: What is difference between weak properties and unowned properties ?
Answer 17: Both weak and unowned reference types do not increase reference count of instance being referred. But there is a subtle difference between two as discussed below
- Weak properties can be nil during the life time of the instance they are member of. Weak reference are used when the instance they are referring to can be released before there instance type thus they can be set to nil. Weak reference can only be var of optional type
- Unowned reference are used when it is not possible for the instance being referred ,to be released before referring instance.Unowned properties can thus be non optional.
Given below a example
class Car{ weak var parkingReceipt:ParkingReceipt! } class ParkingReceipt{ unowned var car : Car init(car:Car){ self.car = car } }
In the example above the ParkingReceipt will always have a shorter life time than the Car thus Car has weak ref of parking receipt where as ParkingReceipt has unowned reference of Car
Question 18: What is @discardableResult used for in swift?
Answer 18: In swift when ever we call a function which returns a value , and we do not use the returned value of function we get a compiler warning. discardableResult attribute marks the return value of the function discardable i.e compiler will not warn you if your choose to ignore the returned value of a function/method marked with discardableResult attribute
func important()-> Bool{ print("hello") return true } @discardableResult func notImportant()->Bool{ print("hello") return false } func test(){ important() // result of call important() is unused warning by compiler notImportant() // no such warning for discardableResult } test()
Code above shows use of discardableResult
Question 19: Remove duplicate characters from a string in Swift.
Answer 19: We can use swift Set to identify and remove duplicate characters from String
let str = "aaabbbcccdddeeeee" var set = Set<Character>() let nonRepeat = String(str.filter{ set.insert($0).inserted } ) print(nonRepeat) // "abcde"
In the example above we have used Set.inserted property to identify duplicate chars
New questions will be added regularly. You can also suggest questions in comment.
Question 20: What are non mutating setters
Answer 20: Nonmutating setters are special tye of setters which do not mutate the instance they are part of rather they have global side effect. Detailed explaination can be found here
Question 21: Which of the following is a valid Numeric literal in swift
let paddedDouble = 000123.456 let oneMillion = 1_000_000 let justOverOneMillion = 1_000_000.000_000_1
Answer 21: All the above. Swift allows the use of leading zeros and “_” for improving the readability of numeric literals