Posts

Showing posts from March, 2018

Swift Interview Questions and Answers

Question 1: class Person { var name : String var age : Int init ( name : String , age : Int ) { self . name = name self . age = age } } let person1 = Person ( name : "John" , age : 26 ) var person2 = person1 person2 . name = "Mike" What's the value of  person1.name  and  person2.name  Would this be any different if  Person  was a class? Why? Both person1.name and person2.name are Mike. Answer: Classes in Swift are reference types, and they are copied by reference rather than value. The following line creates a copy of  person1  and assigns it to  person2 : var person2 = person1 From this line on, any change to  person1  will reflected in  person2 . If  Person  were a structure, person1.name will be  John , whereas person2.name will be  Mike . Structures in Swift are value types. Any change to a property of  person1  would n...