A list of commonly asked iOS interview questions with brief answers:
1. What is the difference between a frame and a bounds in iOS?
- The frame refers to the view's position and size relative to its superview's coordinate system, while the bounds refer to the view's position and size relative to its own coordinate system.
2. How do you implement a delegate in iOS?
- To implement a delegate in iOS, you need to create a protocol that defines the methods that the delegate can implement. Then, you need to create a property in the delegating object to store the delegate and call the delegate methods at appropriate times.
3. Explain the MVC (Model-View-Controller) architecture in iOS.
- MVC is an architectural pattern that separates an app into three components: Model, View, and Controller. The Model represents the data and business logic of the app, the View displays the data to the user, and the Controller acts as the intermediary between the Model and the View.
4. What are the different types of app states in iOS?
- There are five different app states in iOS: Not Running, Inactive, Active, Background, and Suspended.
5. What is the purpose of a retain cycle in iOS and how can it be avoided?
- A retain cycle occurs when two objects hold strong references to each other, causing a memory leak. To avoid a retain cycle, you can use weak or unowned references instead of strong references, or use closure capture lists.
6. What is the difference between a weak and strong reference in iOS?
- A strong reference is a reference that keeps an object alive in memory, while a weak reference is a reference that does not keep an object alive in memory. Weak references are typically used to avoid retain cycles.
7. What is a closure in Swift?
- A closure is a self-contained block of functionality that can be passed around and used in your code. Closures can capture and store references to any constants and variables from the surrounding context in which they are defined.
8. What is the difference between synchronous and asynchronous programming in iOS?
- Synchronous programming waits for a task to complete before moving on to the next task, while asynchronous programming allows tasks to run in the background and notifies the app when they are complete.
9. What is the difference between a struct and a class in Swift?
- Structs are value types, while classes are reference types. Structs are copied when they are passed around, while classes are passed by reference.
10. How do you handle errors in Swift?
- In Swift, you can handle errors using the do-try-catch statement. You can throw an error using the throw statement and catch the error using a catch block.
Sure, here are some more commonly asked iOS interview questions with brief answers:
11. What is a closure capture list in Swift?
- A closure capture list is used to specify which variables and constants the closure should capture and store a reference to. You can use the capture list to specify whether the captured values should be captured as strong or weak references.
12. What is the difference between a segue and an unwind segue in iOS?
- A segue is used to transition from one view controller to another, while an unwind segue is used to transition back to a previous view controller in the navigation stack.
13. What is the purpose of an IBOutlet in iOS?
- An IBOutlet is a special keyword used to mark a property as an interface builder outlet. It allows the view controller to access and manipulate the properties of a view defined in the storyboard.
14. What is the purpose of an IBAction in iOS?
- An IBAction is a special keyword used to mark a method as an interface builder action. It allows the view controller to respond to user interactions with the view defined in the storyboard.
15. What is the difference between a synchronous and asynchronous URL request in iOS?
- A synchronous URL request blocks the current thread until the request is complete, while an asynchronous URL request allows the current thread to continue executing while the request is performed in the background.
16. What is the difference between a view and a layer in iOS?
- A view is a rectangular area on the screen that can contain other views and is responsible for handling user interactions, while a layer is an object that handles the drawing and animation of the view's contents.
17. What is the difference between an atomic and nonatomic property in iOS?
- An atomic property guarantees that the property will be accessed atomically, meaning that it will be thread-safe, while a nonatomic property does not make this guarantee.
18. What is the purpose of the viewDidAppear() method in iOS?
- The viewDidAppear() method is called when a view appears on the screen and is used to perform any necessary setup or updates for the view.
19. What is the difference between a binary and unary operator in Swift?
- A binary operator operates on two operands, while a unary operator operates on a single operand.
20. What is the purpose of the didSet property observer in Swift?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
21. What is the difference between a singleton and a shared instance in iOS?
- A singleton is a class that only allows one instance to be created and provides a global point of access to that instance, while a shared instance is a class that allows multiple instances to be created, but only one shared instance is used throughout the app.
22. What is the purpose of an NSNotification in iOS?
- An NSNotification is used to broadcast a message or data to all interested objects in the app. It allows objects to register themselves as observers and receive notifications when the notification is posted.
23. What is the difference between a push notification and a local notification in iOS?
- A push notification is sent from a server to a device, while a local notification is scheduled and delivered by the device itself. Push notifications require an internet connection, while local notifications do not.
24. What is the purpose of Core Data in iOS?
- Core Data is a framework that provides an object-oriented interface to store, retrieve, and manage data in iOS apps. It allows developers to define the data model and relationships between entities, and provides a way to persist data to a database.
25. What is the difference between an extension and a category in iOS?
- An extension is a feature in Swift that allows you to add functionality to a class, struct, or enum, while a category is a feature in Objective-C that allows you to add functionality to an existing class.
26. What is the purpose of a completion handler in iOS?
- A completion handler is a block of code that is executed when a task is complete, such as when a network request finishes. It allows you to perform additional actions after the task is complete.
27. What is the difference between a guard statement and an if statement in Swift?
- A guard statement is used to check for a condition and exit the current scope if the condition is not met, while an if statement is used to check for a condition and execute a block of code if the condition is true.
28. What is the purpose of an unwind segue in iOS?
- An unwind segue is used to navigate back to a previous view controller in the navigation stack. It is typically used to implement a cancel or done button in a modal view.
29. What is the difference between a protocol and a delegate in iOS?
- A protocol defines a set of methods or properties that a class can implement, while a delegate is an object that implements the methods defined in a protocol on behalf of another object.
30. What is the purpose of the prepare(for segue: UIStoryboardSegue, sender: Any?) method in iOS?
- The prepare(for segue: UIStoryboardSegue, sender: Any?) method is called when a segue is triggered and allows you to pass data or perform other setup before the destination view controller is displayed.
31. What is the difference between a class and a struct in Swift?
- A class is a reference type, while a struct is a value type. This means that when you pass a class instance to a function or assign it to a variable, you are passing or assigning a reference to the object, while with a struct, you are making a copy of the object.
32. What is the purpose of a lazy property in Swift?
- A lazy property is a property that is only initialized when it is first accessed. This can be useful for properties that are computationally expensive to initialize or that are not always needed.
33. What is the purpose of the didSet property observer in iOS?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
34. What is the purpose of the UITableViewDataSource protocol in iOS?
- The UITableViewDataSource protocol is used to provide data to a table view. It defines methods for configuring the content and appearance of the cells in the table view.
35. What is the difference between a break and a continue statement in Swift?
- A break statement is used to exit a loop early, while a continue statement is used to skip to the next iteration of a loop without executing the rest of the loop code.
36. What is the difference between a computed property and a stored property in Swift?
- A computed property is a property whose value is calculated on the fly, while a stored property is a property whose value is stored and retrieved from memory.
37. What is the purpose of the viewDidLoad() method in iOS?
- The viewDidLoad() method is called when a view controller's view is loaded into memory and is used to perform any necessary setup or updates for the view.
38. What is the difference between an implicit and explicit unwrapped optional in Swift?
- An implicit unwrapped optional is a variable or constant that is declared with an optional type but is automatically unwrapped when accessed, while an explicit unwrapped optional is a variable or constant that is explicitly unwrapped using the exclamation mark (!) operator.
39. What is the difference between an unwind segue and a pop view controller in iOS?
- An unwind segue is used to navigate back to a previous view controller in the navigation stack, while a pop view controller is used to remove the current view controller from the navigation stack.
40. What is the purpose of the didSet property observer in iOS?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
41. What is the purpose of an unwind segue in iOS?
- An unwind segue is used to navigate back to a previous view controller in the navigation stack, without creating a new instance of the view controller.
42. What is the difference between synchronous and asynchronous execution in iOS?
- Synchronous execution blocks the current thread until a task is completed, while asynchronous execution allows the current thread to continue executing other tasks while the asynchronous task runs in the background.
43. What is the purpose of the didReceiveMemoryWarning() method in iOS?
- The didReceiveMemoryWarning() method is called when the system detects that the device is running low on memory. It is used to release any resources that are no longer needed to free up memory.
44. What is the difference between a guard statement and an if statement in Swift?
- A guard statement is used to check a condition and exit the current scope if the condition is not met, while an if statement is used to conditionally execute code.
45. What is the purpose of the prepare(for:sender:) method in iOS?
- The prepare(for:sender:) method is called before a segue is performed and is used to pass data between view controllers.
46. What is the purpose of the didSet property observer in Swift?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
47. What is the difference between a closure and a function in Swift?
- A closure is a self-contained block of code that can be passed around and executed at a later time, while a function is a named block of code that can be called and executed.
48. What is the purpose of the delegate pattern in iOS?
- The delegate pattern is used to allow one object to communicate with another object by passing control or data between them.
49. What is the difference between an abstract class and an interface in Swift?
- Swift does not have abstract classes or interfaces, but it does have protocols, which are similar to interfaces in other languages. Protocols define a set of methods and properties that a conforming type must implement.
50. What is the difference between a UIView and a UIViewController in iOS?
- A UIView is a visual element that is used to display content on the screen, while a UIViewController is a container that manages a view and its associated logic.
51. What is the purpose of the didSet property observer in iOS?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
52. What is the difference between a weak and strong reference in iOS?
- A weak reference is a reference that does not keep the referenced object alive, while a strong reference is a reference that keeps the referenced object alive as long as the reference exists.
53. What is the purpose of the UIApplicationDelegate protocol in iOS?
- The UIApplicationDelegate protocol is used to handle system-level events and manage the application's life cycle, such as launching, entering the background, or terminating.
54. What is the difference between a struct and a class in Swift?
- A struct is a value type, while a class is a reference type. This means that when you pass a struct instance to a function or assign it to a variable, you are passing or assigning a copy of the object, while with a class, you are passing or assigning a reference to the object.
55. What is the purpose of a protocol in Swift?
- A protocol is used to define a set of methods and properties that a conforming type must implement. This allows for abstract types and polymorphism.
56. What is the purpose of the guard statement in Swift?
- The guard statement is used to check a condition and exit the current scope if the condition is not met. This is often used to handle error conditions or validate inputs.
57. What is the purpose of the didSet property observer in Swift?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
58. What is the difference between a constant and a variable in Swift?
- A constant is a value that cannot be changed once it is set, while a variable is a value that can be changed.
59. What is the difference between a segue and an unwind segue in iOS?
- A segue is used to navigate to a new view controller, while an unwind segue is used to navigate back to a previous view controller.
60. What is the purpose of the storyboard in iOS?
- The storyboard is a visual representation of the app's user interface and navigation flow. It allows developers to design and prototype the app's UI and connect different view controllers together.
61. What is the difference between a singleton and a shared instance in iOS?
- A singleton is a design pattern that ensures that only one instance of a class can be created, while a shared instance is a class method that returns a global instance of a class.
62. What is the purpose of the didSet property observer in iOS?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
63. What is the difference between a closure and a function in iOS?
- A closure is a self-contained block of code that can be passed around and executed at a later time, while a function is a named block of code that can be called and executed.
64. What is the purpose of the guard statement in iOS?
- The guard statement is used to check a condition and exit the current scope if the condition is not met. This is often used to handle error conditions or validate inputs.
65. What is the difference between a UIView and a CALayer in iOS?
- A UIView is a visual element that is used to display content on the screen, while a CALayer is a low-level object that manages the content of a view.
66. What is the purpose of the didSelectRowAt method in iOS?
- The didSelectRowAt method is called when a table view cell is selected and is used to perform some action, such as navigating to a new view controller or displaying more information.
67. What is the difference between a stack and a queue in iOS?
- A stack is a data structure that follows the last-in, first-out (LIFO) principle, while a queue follows the first-in, first-out (FIFO) principle.
68. What is the purpose of the willResignActive() method in iOS?
- The willResignActive() method is called when the app is about to become inactive, such as when a phone call or notification comes in. It is used to pause any ongoing tasks and save any unsaved data.
69. What is the difference between a synchronous and asynchronous task in iOS?
- A synchronous task blocks the current thread until the task is completed, while an asynchronous task allows the current thread to continue executing other tasks while the task runs in the background.
70. What is the purpose of the viewWillAppear() method in iOS?
- The viewWillAppear() method is called just before a view is about to appear on the screen and is used to update the UI or perform any other tasks that need to be done before the view is shown.
71. What is the difference between a closure and a function in Swift?
- A closure is a self-contained block of code that can be passed around and executed at a later time, while a function is a named block of code that can be called and executed.
72. What is the difference between a synchronous and asynchronous task in Swift?
- A synchronous task blocks the current thread until the task is completed, while an asynchronous task allows the current thread to continue executing other tasks while the task runs in the background.
73. What is the difference between a guard statement and an if statement in Swift?
- A guard statement is used to check a condition and exit the current scope if the condition is not met, while an if statement is used to execute a block of code if a condition is met.
74. What is the purpose of the didSet property observer in Swift?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
75. What is the difference between a protocol and a delegate in Swift?
- A protocol is a set of methods and properties that a conforming type must implement, while a delegate is an object that is responsible for implementing a set of methods that another object can call.
76. What is the purpose of the lazy keyword in Swift?
- The lazy keyword is used to delay the initialization of a property until it is accessed for the first time. This can be useful for expensive or time-consuming operations.
77. What is the difference between a weak and unowned reference in Swift?
- A weak reference is a reference that does not keep the referenced object alive, while an unowned reference is a reference that assumes the referenced object will never be nil.
78. What is the purpose of the didSet property observer in Swift?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
79. What is the difference between a closure and a function in Swift?
- A closure is a self-contained block of code that can be passed around and executed at a later time, while a function is a named block of code that can be called and executed.
80. What is the difference between a stack and a queue in Swift?
- A stack is a data structure that follows the last-in, first-out (LIFO) principle, while a queue follows the first-in, first-out (FIFO) principle.
81. What is the difference between a struct and a class in Swift?
- A struct is a value type, while a class is a reference type. This means that when you create a new instance of a struct, you get a copy of that struct, whereas when you create a new instance of a class, you get a reference to that class.
82. What is a completion handler in Swift?
- A completion handler is a closure that is passed as a parameter to a function and is called when the function has completed its task. Completion handlers are commonly used in asynchronous programming.
83. What is the difference between an implicitly unwrapped optional and a regular optional in Swift?
- An implicitly unwrapped optional is an optional that is automatically unwrapped when it is accessed, while a regular optional must be explicitly unwrapped using the if let or guard let syntax.
84. What is the difference between a computed property and a stored property in Swift?
- A computed property does not store a value, but instead computes a value each time it is accessed, while a stored property stores a value that can be accessed and modified directly.
85. What is the purpose of the didSet property observer in Swift?
- The didSet property observer is used to perform a set of actions when a property is set, such as updating a UI element or performing a calculation.
86. What is the difference between a weak and unowned reference in Swift?
- A weak reference is a reference that does not keep the referenced object alive, while an unowned reference is a reference that assumes the referenced object will never be nil.
87. What is the purpose of the defer keyword in Swift?
- The defer keyword is used to defer the execution of a block of code until the current scope is exited. This can be useful for performing cleanup or other actions that need to be done regardless of whether an error occurred.
88. What is the difference between a dictionary and an array in Swift?
- An array is a collection of elements that are ordered and accessed by index, while a dictionary is a collection of key-value pairs that are unordered and accessed by key.
89. What is a closure in Swift?
- A closure is a self-contained block of code that can be passed around and executed at a later time. Closures can capture and store references to any constants and variables from their context, allowing them to access and modify the values of those variables.
90. What is the difference between a for loop and a forEach loop in Swift?
- A for loop is used to iterate over a sequence of values, while a forEach loop is used to iterate over the elements of a collection and execute a block of code for each element.
Sure, here are some more iOS interview questions with brief answers:
91. What is a protocol in Swift?
- A protocol defines a blueprint of methods, properties, and other requirements that a class, struct, or enum must conform to in order to adopt the protocol. A protocol can also define optional methods or properties.
92. What is the difference between a protocol and an abstract class?
- A protocol is similar to an interface in other programming languages, while an abstract class is a class that cannot be instantiated and must be subclassed. In Swift, protocols can be adopted by any class, struct, or enum, while abstract classes can only be subclassed by other classes.
93. What is the difference between synchronous and asynchronous programming?
- Synchronous programming is when a program executes code in a sequential order, waiting for each line of code to complete before moving on to the next. Asynchronous programming allows code to execute concurrently, with multiple tasks running at the same time.
94. What is a singleton in Swift?
- A singleton is a design pattern in which a class is instantiated only once, and that instance is used throughout the application. The singleton pattern can be used to ensure that there is only one instance of a particular object, such as a database connection or a network manager.
95. What is the difference between a factory method and a constructor in Swift?
- A factory method is a method that is used to create and return instances of a class, while a constructor is a method that is called when an instance of a class is created. The main difference between the two is that a factory method can return different types of objects, depending on the arguments passed to it.
96. What is the difference between a break and a continue statement in Swift?
- A break statement is used to exit a loop or switch statement, while a continue statement is used to skip the current iteration of a loop and move on to the next iteration.
97. What is the difference between a do-try-catch block and a try? statement in Swift?
- A do-try-catch block is used to handle errors that might be thrown by a block of code, while a try? statement is used to attempt a piece of code that might throw an error, and returns nil if the code throws an error.
98. What is the difference between a delegate and a notification in iOS?
- A delegate is a design pattern in which one object delegates certain responsibilities to another object, while a notification is a mechanism for broadcasting information to any objects that are interested in that information.
99. What is the difference between KVC and KVO in iOS?
- KVC (Key-Value Coding) is a mechanism for accessing an object's properties indirectly, by using key paths instead of direct property access. KVO (Key-Value Observing) is a mechanism for observing changes to an object's properties, and receiving notifications when those properties change.
100. What is the difference between a push notification and a local notification in iOS?
- A push notification is a message that is sent from a remote server to an iOS device, while a local notification is a message that is scheduled and delivered locally on the device. Push notifications require an internet connection and a server to send the notification, while local notifications are scheduled and delivered by the device itself.
101. What is a closure in Swift?
- A closure is a self-contained block of code that can be passed around and used in your program. It captures the state of the variables and constants in the enclosing context, and can be executed later. Closures are similar to blocks in Objective-C or lambdas in other languages.
102. What is the difference between a value type and a reference type in Swift?
- A value type is a type that is copied when it is assigned to a new variable or passed as an argument to a function. A reference type, on the other hand, is a type that is passed by reference, and multiple variables can refer to the same object.
103. What is a guard statement in Swift?
- A guard statement is a control flow statement that is used to ensure that a certain condition is true before continuing with the execution of a function. If the condition is false, the guard statement exits the function early, usually by using the return keyword.
104. What is the difference between a struct and a class in Swift?
- A struct is a value type that is passed by copy, while a class is a reference type that is passed by reference. Structs are usually used for simple data types, while classes are used for more complex objects that need to be shared across multiple parts of your program.
105. What is the difference between a view and a layer in iOS?
- A view is a user interface element that is used to display content and receive user input. A layer is a low-level object that is used to draw content onto a view, and can be used to perform advanced animations and visual effects.
106. What is a closure capture list in Swift?
- A closure capture list is used to explicitly specify which variables or constants should be captured by a closure. This can be useful to avoid capturing variables that might cause retain cycles or to improve the performance of the closure.
107. What is the difference between a private and a fileprivate access level in Swift?
- A private access level restricts access to a property or method to the current file or scope, while a fileprivate access level restricts access to the current file only.
108. What is a lazy property in Swift?
- A lazy property is a property that is not initialized until it is first accessed. This can be useful for properties that are expensive to initialize or that might not be used in every case.
109. What is the difference between a weak and an unowned reference in Swift?
- A weak reference is a reference to an object that does not prevent the object from being deallocated. An unowned reference is a reference to an object that assumes the object will never be deallocated. The main difference between the two is that a weak reference is optional, while an unowned reference is non-optional.
110. What is the difference between a deep copy and a shallow copy in Swift?
- A deep copy creates a new copy of an object and all of its sub-objects, while a shallow copy creates a new copy of an object but shares the same sub-objects with the original object. In Swift, value types are usually deep-copied, while reference types are usually shallow-copied.