iOS Interview Questions ( Part 1 )
Hello! This series of articles will be an important resource for iOS developers. In each installment, we will tackle three questions that are commonly encountered in iOS interviews and are critical for successful performance. Our aim is to guide developers preparing for interviews and provide valuable tips for those aiming for a successful career on the iOS platform.
In this series of articles, we will address three fundamental topics that are commonly encountered and considered important in iOS interviews. First, we will examine the differences between struct and class, which are basic concepts in the Swift programming language. Next, we will focus on IBOutlet and IBAction, which are frequently used when creating interfaces with Storyboard. Finally, we will explore the role of AppDelegate, which manages the lifecycle of applications and performs fundamental functions. As we explain each topic, we will support them with examples to ensure better understanding of the concepts. Now, let’s dive into these important topics in detail to enhance the knowledge of iOS Developers !
1-) What are the differences between a Swift Struct and a Class ?
Struct :
- Structs are value types.
- They are typically used for small and simple data structures.
- When passed around in code, structs are copied, so each instance has its own unique copy.
- They cannot inherit properties or functionality from other structs.
Class :
- Classes are reference types.
- They are used for more complex data structures and objects.
- Classes are passed by reference, so multiple references can point to the same instance.
- They support inheritance, allowing subclasses to inherit properties and functionality from superclass.
In Swift, structs and classes can be used for similar purposes, but there are some fundamental differences:
- Value Types vs Reference Types :
Structs
are value types, whileclasses
are reference types. This means that when assigning onestruct
to another, values are copied, whereas with classes, references are copied. - Mirroring and Inheritance :
Structs
do not support inheritance and do not have mirroring capabilities.Classes
, on the other hand, support both inheritance and mirroring. - Deinitializer :
Classes
have deinitializer methods, whereasstructs
do not support this feature.
Code :
// Example of Struct
struct Point {
var x: Int
var y: Int
}
// Example of Class
class Person {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
}
var point1 = Point(x: 10, y: 20)
var point2 = point1
point2.x = 30
var person1 = Person(name: "John", age: 30)
var person2 = person1
person2.name = "Alice"
print(point1) // Output: Point(x: 10, y: 20)
print(point2) // Output: Point(x: 30, y: 20)
print(person1.name) // Output: Alice
print(person2.name) // Output: Alice
2-) What do IBOutlet and IBAction mean, and how are they used ?
IBOutlet and IBAction are special markers used to connect interface files (Storyboard or XIB) to Swift code.
- IBOutlet : IBOutlet is used to associate an instance of an interface element (
UIButton
,UILabel
,UITextField
, etc.) with a variable in Swift code. This allows access to interface elements from code. - IBAction : IBAction is used to connect an event of an interface element. (
UIButton
,UIBarButtonItem
, etc. ), such as a button click, to a Swift method. This enables handling of interactions in code.
Code :
class ViewController: UIViewController {
@IBOutlet weak var myLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// When the interface is loaded, the actions to be performed are written here.
}
@IBAction func buttonTapped(_ sender: UIButton) {
myLabel.text = "Button tapped!"
}
}
In this example, a UILabel
instance named myLabel is created with @IBOutlet and linked to a label in Interface Builder. Additionally, a method named buttonTapped is created with @IBAction and linked to a UIButton
. This allows updating the myLabel label when the button is tapped.
3-) What is the purpose of the AppDelegate in an iOS application ?
The AppDelegate is a class in iOS applications that manages the lifecycle of the application and handles system events. This class contains special methods that are called when the application starts, pauses, and terminates.
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Perform any setup tasks here when the application is launched.
// Example: Checking user session and performing auto-login.
// Example: Loading user preferences and configuring the app interface accordingly.
// Example: Loading app themes.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Perform tasks here when the application is about to move from active to inactive state.
// Example: Pausing video playback.
// Example: Pausing animations.
// Example: Pausing game flow in games.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Perform tasks here when the application enters the background.
// Example: Saving data (e.g., saving the page where the user was logged in or local data).
// Example: Initiating background tasks (e.g., receiving notifications or tracking location).
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Perform tasks here when the application is about to enter the foreground.
// Example: Redirecting the user to login.
// Example: Showing important update messages to the user.
// Example: Checking ongoing background tasks.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Perform tasks here when the application becomes active.
// Example: Returning to the last used screen in the application.
// Example: Checking and, if necessary, restarting ongoing background tasks.
// Example: Checking user session and performing auto-login.
}
func applicationWillTerminate(_ application: UIApplication) {
// Perform tasks here when the application is about to terminate.
// Example: Saving data in the final state (e.g., saving the page where the user last performed an action or local data).
// Example: Terminating ongoing background tasks.
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
In this example, the AppDelegate class contains methods used to manage various lifecycle events of the application such as its launch, backgrounding, foregrounding, becoming active, and termination. These methods can be utilized to perform various functionalities related to the application state.
This article series will be an important resource for iOS developers. In each installment, we will address three questions commonly encountered in iOS interviews and crucial for successful performance. Our aim is to guide developers preparing for interviews and provide valuable tips for those aiming for a successful career on the iOS platform.
At the end of the article, with careful attention to supporting these fundamental topics with examples for better understanding, we will delve into these important areas in detail to enhance the knowledge of iOS Developers.
Additionally, as a continuation of this article series, a new installment will be published every week. So, next week, you will find here an article titled “iOS Interview Questions (Part 2)”. Each new installment will present new questions and tips that iOS developers can benefit from while preparing for interview processes. Stay tuned for fresh content every week!
We hope these articles will be a valuable resource for your iOS development journey and assist you in achieving success in interview processes. Wishing this serves as a guiding resource for beginners as well!
Wishing you all successful development experiences!
You can check the Turkish version.
Don’t forget to follow for iOS Interview Questions (Part 2) !
Follow me on GitHub : https://github.com/keremdemirios
Follow me on LinkedIn : https://www.linkedin.com/in/kerem-demir-b31a69190/