Swift-iOS Interview Questions-Part 7

Baljit Kaur
Swift Interview Preparations
8 min readMay 26, 2024

Interview Questions about swift, iOS, Xcode

1. How can we create optional?

optional in swift is to add ? in front of the type e.g

var name: String?

2. What is optional Binding?

Optional Binding is used to find out whether an optional contains a value or not if the value is available then hold it in a constant or in a variable with the help of if let. constant and variable created in the if statement available only in the if block.

We are going to take optional value and we are going to bind it non optional constant. We used If let structure or Guard statement.

3. What is optional chaining?

It is the process for querying And calling properties, subscripts, and methods on an optional which may currently be nil. If the optional the property method, value or subscript call succeeds. In case the optional is All property, methods or subscript call returns nil. If multiple queries chain together and one of the links in the chain is nil then the entire chain fails gracefully.

In Swift programming language, Optional Chaining is a process of querying and calling properties. You can chain multiple queries together, but if any link in the chain is nil then, the entire chain fails.

4. Explain the Design pattern used in iOS application development?

In essence, design patterns are repeatable code solutions that may be applied repeatedly to address typical software issues. Using design patterns in your projects will result in more modular, more scalable, and optimized software. You’ll better comprehend other people’s code since you’ll be able to recognize the design pattern right away.

Design patterns do not serve as guidelines for creating better software, as design patterns and best practices are totally unlike one another. Moreover, they aren’t intended to provide instructions on how to approach challenges. Rather, they merely serve as documentation of observed usual responses to typical engineering and architectural concepts. Common design patterns include Facade, Decorator, Factory Method, Singleton, etc.

Following design patterns are used commonly in iOS development.

  • Creational design pattern — Builder, Factory, and Singleton.
  • Structural design pattern Decorator — facade, adapter and Decorator.
  • Behavioral design pattern — observer and memento

5. What are the various ways to unwrap an optional value in swift.

  • Guard statement (safe way)
  • Optional Binding — safe
  • Optional Pattern — safe
  • Forced unwrapping — using “!” operator, unsafe.
  • Optional chaining — safe
  • Nil coalescing operator — safe
  • Implicitly unwrapped variable declaration: unsafe in many cases.

6. Nil coalescing operator

Use of ?? where if the left side of the operator is nil, then the value to the right of ?? is used.

7. What is core data in iOS?

Core data is a framework provided by Apple to save, fetch, delete, and modify the data. it is used to manage the model layer object in the application. Core data is not a database. With the help of core data’s data model editor, we can easily define our data type and relationship, and we can also generate the respective class definitions.

Core Data is not an ORM or object-relational mapper. Nor is it a database. Instead, Core Data is an object graph manager which also has the ability to persist object graphs to a persistent store, on a disk.

Apple Says : “Use Core Data to save your application’s permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device.” 😮

Core data gives you these features : Persistence , Undo and Redo of Individual or Batched Changes , Background Data Tasks , View Synchronization , Versioning and Migration etcc.. .

You can create core data model while creating project by check the box “use core data” .

Core Data Stack

After you create a data model file , set up the classes that collaboratively support your app’s model layer. These classes are referred to collectively as the Core Data stack .

There are few Core Data Components :

  • An instance of NSManagedObjectModel represents your app’s model file describing your app’s types, properties, and relationships.
  • An instance of NSManagedObjectContext tracks changes to instances of your app’s types.
  • An instance of NSPersistentStoreCoordinator saves and fetches instances of your app’s types from stores.
  • An instance of NSPersistentContainer sets up the model, context, and store coordinator all at once.

8. Different Data Types in Core Data

Many apps need to persist and present different kinds of information. Core Data provides different attributes, including those common for all databases, such as Date or Decimal type, and non-standard attributes handled with Transformable type.

9. What is a managed object context?

A managed object context represents a single object space, or scratch pad, in a Core Data application.

10. What is the difference between static Binding And Dynamic Binding?

Static Binding: Event Occurs at compile time. Execution is fast in static binding. There is early Binding. It is resolved at “Compile-time” Method overloading is an example of static binding.
Dynamic Binding: Event occurs At run time Execution is slow in dynamic binding There is late Binding. It is virtual binding resolved at a “Run Time”. Method overriding is an example of Dynamic Binding.

11. What is Entity Inheritance?

Entity inheritance works similarly to class inheritance. Sometimes we have a large number of entities and some of them are similar. In this case, we can factor the common properties into the supernity and we can say it is the parent entity. It helps to reduce the work when subentities inherit the parent entity.
Example: As we defined an entity as a person name with attributes first Name and Last Name and sub-entity employee and customer can inherit those attributes.

12. Multithreading can be possible in core Data?

Yes, multithreading can be possible in core Data.

13. What is Deinit?

DeInitializer is called immediately before an instance of a class is deallocated. The keyword used by DeInitializer is deinit similar to the initializer used with Init keyword. Important thing is that DeInitializer is only available for class type not for structure type.

14. What is the purpose of the reuseIdentifier?

Reusability of an already allocated object.

The ‘reuseIdentifier’ is used to group all the similar rows from UITableView.

15. How many UITableViewCells are allocated when you first load a UITableView? How many additional ones are allocated as you scroll through the table?

A UITableView will normally allocate just enough UITableViewCell objects to display the content visible in the table. Because of the reuseIdentifier , the UITableView will not allocate new UITableViewCell objects for each new item that scrolls into view, avoiding laggy animations.

16. Why we use reuse identifier in the UITableViewCell constructor?

If we do not set a reusableidentifier then Tableview will force to allocate new uitableviewcell.
The use of ReuseIdentifier is that it is used to group similar rows content in an uitableView but they can differ in data content.

17. What are generics and which problem do they solve?

Generic code Helps you to write reusable, flexible functions And type that can work with any type. Generics are the most powerful feature of the Swift language.

18. What is @synthesize in Objective-C?

@synthesize tells the compiler to generate the setter and getter property for the variable.

19. What is ARC? How does it help manage memory in Swift? Provide an example to illustrate its functionality.

ARC: Automatic Reference Count is used in iOS Application to track and manage your application memory. And Arc automatically manages the memory it automatically releases the memory held by the class instance when it is no longer needed.

class Person {

let name: String

init(name: String) {

self.name = name

print(“\(name) is being initialized.”)

}

deinit {

print(“\(name) is being deinitialized.”)

}

}

var person1: Person? = Person(name: “Alice”)

var person2: Person? = person1 // person1 and person2 now both reference the same Person instance

person1 = nil // The reference count of the Person instance decreases to 1

person2 = nil // The reference count of the Person instance decreases to 0 and the instance is deallocated

In this example, when person1 and person2 are assigned to the same Person instance, the reference count becomes 2. When both person1 and person2 are set to nil, the reference count drops to 0, and the Person instance is deallocated, triggering the deinitializer.

20. What is Live Rendering?

The Attributes IBDesignable and IBInspectable used by the Interface builder, which helps to directly render the element in the interface builder, for specific attributes like colors/widths/shadows and border to be configured live in the storyboard.

21. Differentiate between the ‘assign’ and ‘retain’ keywords.

Retain — specifies that retain should be invoked on the object upon assignment. It takes ownership of an object.

Assign — specifies that the setter uses simple assignment. It is used on attributes of scalar type like float, int.

22. What are iBeacons?

iBeacon.com defines iBeacon as Apple’s technology standard which allows Mobile Apps to listen for signals from beacons in the physical world and react accordingly. iBeacon technology allows Mobile Apps to understand their position on a micro-local scale, and deliver hyper-contextual content to users based on location. The underlying communication technology is Bluetooth Low Energy.

Apple’s introduction of Bluetooth low-energy (BLE) wireless technology, iBeacon, is a new way for iPhones and other iOS users to receive location-based information and services.

23. Define Autorelease ?

By Sending an object an autorelease message, it is added to the local AutoReleasePool, and you no longer need to worry about it, because when the AutoReleasePool is destroyed(on main run loop), the object will receive a release message(retain count is decremented by one) and the garbage collector will destroy the object if the RetainCount becomes zero.

Release: retain count is decrease by one when we send release message to object.

24. What is an autorelease pool?

Every time — autorelease is sent to an object, it is added to the inner-most autorelease pool. When the pool is drained, it simply sends — releases to all the objects in the pool. You utilize @autoreleasepool blocks.

Autorelease pools are a convenience that allows you to defer sending -release until “later”. That “later” can happen in several places, but the most common in Cocoa GUI apps is at the end of the current run loop cycle.

25. How to accessing variable in blocks?

By using __block Storage Type

If you enjoyed reading this article, please share and give claps so others can find it👏🏻👏🏻👏🏻👏🏻👏🏻

You can also connect with me on📲

LinkedIn

You can have a look at my code from down below👇🏻

GitHub

Find it a good read?

If you have any comments, questions, or recommendations, feel free to post them in the comment section below💬

💁🏻‍♀️Happy coding!

Thanks😊

Follow Swiftfy for more updates!

Reference for more articles:

--

--

Baljit Kaur
Swift Interview Preparations

iOS Developer  since 2021 || Writing a new blog post every day related to Swift, SwiftUI, iOS and Xcode || Passionate Professional