Top IOS Interview Questions and Answers (August 2018) Part 4

This articles end the questions for this month, next month will come with more new questions.

Top IOS Interview Questions and Answers (August 2018) Part 4

This articles end the questions for this month, next month will come with more new questions.

Q1. Whether below snippets is correct and will not give compiler error? Do we need to remove class keyword when overriding in class B?

Solution:

Yes its correct and compiled correctly, If you remove class keyword in override func in class B, it will throw error.

removed class keyword error

Q2. In below snippet, two scenarios
a. What if we add class keyword before func convert.. what that means & will it compile ?
b. What if we add static keyword before func convert.. what that means & will it compile ?

Solution:

a. It will give compile time error

b. it will compile perfectly

what that means covered in our next question.

Q3. What is the difference between static func and class func in Swift?

Solution: static functions of structs and enums, and class for classes

Some other differences are that class functions are dynamically dispatched and can be overridden by subclasses.

Q4. What is the Class Only Protocols, can you write code for it?

Solution:
You can limit protocol adoption to class types (and not structures or enumerations) by adding the AnyObject protocol to a protocol’s inheritance list.

Q4. Write generic Singelton class?

Solution: Classes which conforms to Initializable protocol will have one instance through out the life cycle.

Use case:

Solution:
Is it simply that static is for static functions of structs and enums, protocols and class for classes?
That's the main difference. Some other differences are that class functions are dynamically dispatched and can be overridden by subclasses.