5 Random iOS Developer Interview Questions

Hi all!

Let's look at 5 questions that you might be asked at an interview for an iOS developer position. Most likely, these are Junior-level questions, but since it is currently unclear who is asking what and from whom, we will not start a holy war 🙂

Question #1: Is it possible to create optional methods in protocols?

Answer: There are two ways to create optional methods for protocols.

  1. Using a keyword optional.

@objc protocol NameProtocol {
    @objc optional func nameMethod()
}

class NameClass: NameProtocol {
}

Pros:
– No need to specify default implementation

Cons:
– The optional keyword is only available for the @objc protocol. This means that only classes inheriting from NSObject can conform to the protocol. Structures and enumerations cannot conform to the protocol.
– Before calling, you need to check whether this method is implemented.

  1. Default implementation

protocol NameProtocol {
    var nameProperty: String { get set }
}

extension NameProtocol {
    func nameMethod() {
        print("default implementation")
    }
}

class NameClass: NameProtocol {
    var nameProperty: String = "name"
}

Pros:
– Classes, structures, and enumerations can conform to the protocol.
– Possibility of using Generics.
– Confidence that there is either a native implementation of the method or a default implementation.

Cons:
– It is not always possible to write a universal default implementation.
– It is impossible to distinguish between a default implementation and no implementation.

Question #2: How is static different from class?

Answer: static and class are quite similar, but there are differences.

Similarities:
– static and class make the property/method they are applied to a property/method of the type. Such a property/method can be called directly, without creating an instance.

class Car {
    static let wheels = 4
}
Car.wheels

Difference:
– class can only be applied to computed properties
– class can only be applied to class properties and methods
– class allows you to override a property/method

class Car {
    class var wheels: Int {
        4
    }
}

class Mercedes: Car {
    override class var wheels: Int {
        3
    }
}

Question #3: Can lazy computed properties be evaluated more than once?

Answer: No

lazy var – a variable that is initialized only on first access.
The initialization code is executed only once. The result is stored in a variable.
The next time you access it, the saved value will be returned.

class NameClass {
    lazy var lazyProperty: Int = {
        print("lazyProperty")
        return 0
    }()
}

let instance = NameClass()
instance.lazyProperty
instance.lazyProperty
instance.lazyProperty

//Консоль
//lazyProperty

The message “lazyProperty” will be printed to the console only once, when lazyProperty is initialized. On subsequent calls, the saved value is returned.

A more interesting example:

class NameClass {
    var a: Int
    var b: Int
  
    lazy var lazyProperty: Int = {
        a + b
    }()
  
    init(a: Int, b: Int) {
        self.a = a
        self.b = b
    }
}

let instance = NameClass(a: 10, b: 2)
instance.lazyProperty
instance.a = 20
instance.b = 15
print(instance.lazyProperty)

//Консоль
//12

The console will display the value 12, calculated and saved during the first access.

Question #4: Why can't memberwise initializer be called if it contains at least one property with private level?

Answer: When you run the code below, you will get an error.

struct NameStruct {
    private let first: Int
}

let nameStruct = NameStruct(first: 1)

The error description states that this initializer has private access level, so it cannot be called.

This behavior is due to the fact that the memberwise initializer sets the values ​​directly. Since the property has a private access level, it becomes impossible to set the value from outside. The initializer is assigned a private access level.

Question #5: Why don't classes have a memberwise initializer like structs?

Answer: When answering this question, I suggest referring to arguments Chris Lattner.

  1. When you implement your own memberwise initializer, the initializer is gone. There is no easy way to get it back.

Example with structure:

struct NameStruct {
    let first: String
    let second: String

    init(first: String) {
        self.first = first
        self.second = "two"
    }
}

let nameStruct = NameStruct(first: "1", second: "2") //error
  1. Access control. For properties with private memberwise access level, the initializer requires a default value to be set. If at least one of the initializer's members has private access level, the initializer will also have private access level and will not be available for use. (Question #4)

  2. A memberwise initializer must be able to set a default value for variables.

  3. memberwise initializer captures lazy vars

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *