Swift Interview Questions and Answers Part 1.
What is the difference between a struct and a class in Swift?
- A struct is a value type that is copied when it is assigned to a variable or constant.
- A class is a reference type that is passed by reference. Structs have value semantics, while classes have reference semantics.
How do you handle optional values in Swift?
- Optionals are used to represent the absence of a value in Swift. You can use the "if let" or "guard let" statement to unwrap an optional value, or use the "??” operator to provide a default value.
- What is the use of the guard statement in Swift?
- The guard statement is used to exit a function or method early if a condition is not met. It is useful for early exits when a function or method is passed an invalid parameter or state.
- How do you implement error handling in Swift?
- Swift has built-in support for error handling using the "do-catch" statement. You can use this statement to catch and handle errors that are thrown by a function or method. You can also define your custom error types that conform to the Error protocol.
- What are the key features of the Swift programming language?
- Some of the key features of Swift include: type inference, optionals, closures, functional programming patterns, automatic reference counting, and tuples.
- How do you implement a protocol in Swift?
- To implement a protocol in Swift, you define the protocol using the "protocol" keyword, and then implement the required properties and methods in a class, struct, or enum. To conform to a protocol, the type uses the ": ProtocolName" syntax in its definition.
- What is the difference between synchronous and asynchronous code in Swift?
- Synchronous code is code that runs in a linear, blocking manner, meaning the next line of code will not be executed until the current line of code has completed.
- Asynchronous code, on the other hand, runs concurrently, meaning multiple lines of code can be executed at the same time, allowing for non-blocking execution.
- Can you explain the difference between a lazy variable and a computed property in Swift?
- A lazy variable is a variable that is only initialized when it is first accessed, while a computed property is a property that is calculated when it is accessed. Lazy variables are useful for properties that take a long time to initialize or that are only needed in certain situations, while computed properties are useful for properties that need to be recalculated each time they are accessed.
- What is the difference between a protocol and an interface in Swift?
- A protocol defines a blueprint of methods, properties, and other requirements that a class, struct, or enumeration must implement, while an interface defines methods and properties that a class must implement.
- In Swift, protocols are used for defining contracts for types, while interfaces are not available, because Swift protocols can do the same job as interfaces do in other languages.
- What is the difference between a weak and an unowned reference in Swift?
- A weak reference is a reference that does not maintain an object's reference count and allows the referenced object to be deallocated,
- Weak references are used when the reference is optional, and the reference's lifetime may end before the referenced object
- An unowned reference is similar to a weak reference, but it assumes that the referenced object will always exist.
- unowned references are used when the reference is non-optional, and the reference's lifetime is guaranteed to end before the referenced object.
- Can you explain the difference between map, filter, and reduce in Swift?
- Map, filter, and reduce are higher-order functions in Swift that operate on collections.
- Map applies a given function to every element of a collection.
- Filter selects elements from a collection that satisfy a given condition, and reduce combines all elements in a collection to a single value.
- Can you explain the difference between a mutable and an immutable collection in Swift?
- A mutable collection is a collection that can be modified after it is created.
- An immutable collection is a collection that cannot be modified after it is created.
- In Swift, arrays and dictionaries are mutable collections, while sets are immutable.
- What is the difference between value type and reference type in Swift?
- A value type holds the actual data, while a reference type holds a reference to the memory location where the data is stored.
- When you pass a value type to a function, the function receives a copy of the data, while when you pass a reference type, the function receives a reference to the data.
- In Swift, structs and enums are value types, while classes, closures & actors are reference types.
- What is the purpose of the defer statement in Swift?
- The defer statement is used to execute a block of code just before the current scope is exited.
- This is useful for cleaning up resources, such as closing file handles or releasing memory. The defer statement can be used in conjunction with the guard statement to ensure that cleanup code is executed even if an error is thrown.
- Can you explain the difference between a protocol extension and a class extension in Swift?
- A protocol extension allows you to add methods, properties, and other functionality to an existing protocol.
- Class extension allows you to add methods, properties, and other functionality to an existing class. Protocol extensions can be used to add default implementations to protocols, while class extensions can be used to add additional functionality to classes without subclassing.
- What is the difference between the @escaping and @non-escaping keywords in Swift?
- The @escaping keyword is used to indicate that a closure passed as an argument to a function may be executed after the function returns.
- @non-escaping keyword is used to indicate that a closure passed as an argument to a function will be executed before the function returns.
- The @escaping keyword allows the closure to "escape" the function, while the @non-escaping keyword ensures that the closure will be used only within the function.
- How do you implement generics in Swift?
- Generics in Swift allow you to write flexible and reusable code. You can create generic functions, methods, and types by using a placeholder type called a "generic type parameter" indicated by a single letter in angle brackets <>. When you call a generic function or use a generic type, you specify the type to be used in place of the generic type parameter.
- How do you use the "self" keyword in Swift?
- The "self" keyword is used to refer to the current instance of a class, struct, or enumeration.
- The "self" keyword is used to distinguish between properties and methods of the current instance and those with the same name in a superclass or other context.
- What is the difference between a "for-in" loop and a "while" loop in Swift?
- A "for-in" loop is used to iterate over a sequence of items, such as an array or a range.
- A "while" loop is used to execute a block of code repeatedly as long as a certain condition is true.
- The "for-in" loop is useful for iterating over a known set of items, while the "while" loop is useful for situations where the number of iterations is unknown or based on a certain condition.
- How do you use the "typealias" keyword in Swift?
- The "typealias" keyword is used to give a new name to an existing type. It allows you to create a new name for an existing type, making the code more readable and reducing the need for complex type declarations.
- It can be used to create an alias for complex generic types, or to create an alias for a type that is used multiple times in a codebase.
- How do you use the "extension" keyword in Swift?
- The "extension" keyword is used to add new functionality to an existing class, struct, or enumeration. It allows you to add new methods, properties, and initializers to a type without modifying its original code.
- The extension can be used to organize your code, add protocol conformance to a type, or to add computed properties to a struct or class.
- Swift Interview Questions and Answers Part 2
Comments
Post a Comment