10X Sale
kh logo
All Courses

Introduction

Swift is widely adopted by iOS developers as the primary programming language used to build iOS applications, in addition to server-side and web development in recent times. It offers many benefits for developers, including faster development times, better performance, and improved safety and security. Be among the selected list of candidates with our sample Swift interview questions and answers. If you are looking to build your career as a Swift developer, then prepare in advance with these frequently asked interview questions in Swift. These Swift interview questions and answers cover topics like dump function, view model in MVVM and controller in MVC, etc. With these Swift interview questions, you can be confident that you will be well-prepared for your next interview. So, if you are looking to advance your career in iOS development, this guide is the perfect resource for you. Be prepared and chase your dream job.

Swift Interview Questions and Answers
Intermediate

1. Please consider the following code: (Advanced)

enum BookType {
    case biology
    case physics
    case maths
    case unknown
}
class Book {
    var name:String?
    var type:BookType = .unknown
}
class BookStorage {
        func fetchAllBooks() -> [Book] {
            // return all available books
        }
  }
class BooksManager {
        func physicsBooks() -> [Book] {
            let books = BookStorage().fetchAllBooks()
            // identify the physics book and return those
            let physicsbooks …
            return physicsbooks
        }
}

We want to test cover the function of physicsBooks.  Test the logic if it identifies the physics books from different data sets. Redesign this classes to test cover the method.

Use dependency injection to redesign the classes and provide data from test methods

protocol IBookStorage {
func fetchAllBooks()->[Book]
}
class BookStorage: IBookStorage  {
    func fetchAllBooks() -> [Book] {
        // return all available books
    }
}



 class BooksManager {
        let bookStorage:IBookStorage
        init(bookStorage:IBookStorage) {
            self.bookStorage = bookStorage
        }
        func physicsBooks() -> [Book] {
            let books = bookStorage.fetchAllBooks()
            // identify the physics book and return those
            let physicsbooks = …
            return physicsbooks
        }
    }
class StubBookStorage: IBookStorage  {
    var books = [Book]()
    init(books:[Book]) {
        self.books = books
    }
    func fetchAllBooks() -> [Book] {
        return books
    }
}
// Test methods
   func testBookManager() {
let books = …  // add stub data you want to test
let bookStorage = StubBookStorage(books: books)
let bookManager = BooksManager(bookStorage: bookStorage)
let physicsBooks = bookManager.physicsBooks()
// test the value physicsBooks array if correct
    }

This, along with other Swift basic questions for freshers, is a regular feature in Swift programming interviews, be ready to tackle it with the approach mentioned.

2. Consider a scenario where I have two different system storage which requires IDs in different data types. One system will deal int values only and others in string values only. Create a generic protocol with a single property “ids” ( holds an array of ids) and a function to add id, that will be used for two systems.

Key is to use associated typeswhich is a powerful way of making protocols generic.

protocol Store {
    associatedtype DataType
    var ids: [DataType] { get set}
    mutating func add(id: DataType)
}
extension Store {
    mutating func add(id: DataType) {
        ids.append(id)
    }
}
struct SystemOne: Store {
    var ids = [String]()
}
struct SystemTwo: Store {
    var ids = [Int]()
}
var sysOne = SystemOne()
sysOne.add(id: "One")
sysOne.add(id: "Two")
var sysTwo = SystemTwo()
sysTwo.add(id: 1)
sysTwo.add(id: 2)
print(sysOne.ids)
print(sysTwo.ids)

3. What is the exact difference in the view model in MVVM and controller in MVC? Which pattern is preferable keeping in mind the code unit test coverage?

In MVC, controller is coordinated that reads / saves data into the data model and displays data / handles actions in the view.

In MVVM, the view model is a close representation of a view. The view model is bound to the view. Any change in data in the view model, leads to notifiications and view pulls data from the view model and it displays it.

Test covering controllers can be a bit difficult as view is tightly attached to a view i.e View controller will have an instance of a view within it. So it will be difficult to mock the view methods called from the controller.

Test covering in  ViewModel is rather easy as it does not contain any reference to a view. Rather it only prepares data for the view only. It is the view that pulls the data from the view model.  Also, the view delegates the action to the View model. 

One of the most frequently posed Swift iOS interview questions, be ready for this conceptual question.

Description