Top 10 Swift Interview Questions with Answers (2024 Update)
1. What is the difference between a value type and a reference type in Swift?
- Value type: A value type is copied when assigned to another variable or passed to a function. Examples include Int, Double, String, and struct.
- Reference type: A reference type holds a reference to the memory location of the actual data. Changes made to a reference type variable after assignment or passing will affect the original data. Examples include class, NSArray, and NSDictionary (these are Objective-C classes used in Swift).
2. Explain optionals and how they handle nil values.
- Optionals are a way to represent values that may or may not exist. They are declared with a question mark (?) after the type name. You can use optional chaining (?.) to access properties or call methods on an optional value safely, avoiding crashes if the value is nil.
3. What are closures in Swift, and how are they used?
- Closures are self-contained blocks of code that can capture and store references to any variables from their surrounding context. They are often used as callback functions, passed as arguments to other functions, or for defining inline behavior.
4. Explain the difference between struct and class in Swift.
- Both struct and class are used to define custom data types. However, there are some key differences:
- Value vs. reference: By default, structs are value types, while classes are reference types.
- Inheritance: Classes can inherit properties and methods from other classes, while structs cannot.
- Mutability: Structs are immutable by default, meaning their properties cannot be changed after they are initialized. Classes are mutable by default.
5. Describe how memory management works in Swift, including Automatic Reference Counting (ARC).
- Swift uses Automatic Reference Counting (ARC) to manage memory automatically. ARC keeps track of how many strong references there are to an object and deallocates the object when there are no more strong references.
6. What are protocols in Swift, and how do they promote code reuse and polymorphism?
- Protocols define a blueprint of methods, properties, and requirements that a type must conform to. Protocols enable code reuse by allowing different types to implement the same functionality and promote polymorphism by allowing you to treat objects of different types that conform to the same protocol in a similar way.
7. Explain generics in Swift and their benefits.
- Generics allow you to write code that works with a variety of data types without needing to duplicate the code for each type. This improves code reusability and maintainability.
8. Describe how error handling is done in Swift using try, catch, and throw keywords.
- Swift uses try, catch, and throw keywords for error handling. try is used to call a function that might throw an error. throw is used to throw an error from a function. catch is used to handle the thrown error within a do block.
9. What are the different types of collections available in Swift?
- Swift provides various collection types like arrays, dictionaries, sets, and ranges, each with its own specific characteristics and use cases.
10. Discuss the difference between value vs. reference equality in Swift, and how to check for both.
- Value equality: Two values are considered equal if they have the same value. The == operator checks for value equality for value types.
- Reference equality: Two references are considered equal if they point to the same memory location. The === operator checks for reference equality for both value and reference types.