Skip to content
  • Home
  • About us
  • Contact
  • Privacy Policy
  • Disclaimer
  • Swift Online Compiler
iOS Interview Questions and Tutorials

iOS Interview Questions and Tutorials

Data Source and Delegate in Swift

Posted on December 16, 2023July 23, 2024 By Sid No Comments on Data Source and Delegate in Swift
Data Source and Delegate in Swift

Table of Contents

Toggle
  • Data Source and Delegate in Swift
    • 1. Delegate :
      •       Pattern: 
      •        Purpose:
      •         Example:
    • 2. Data Source:
      • Pattern: 
      • Purpose: 
      • Example:
  • Read More:

Data Source and Delegate in Swift

Data Source and delegate in Swift, both are design patterns that facilitate communication and interaction between objects. Here’s a concise summary of the differences between a data source and a delegate:

1. Delegate :
      Pattern: 
    • A delegate is a design pattern where one object delegates some of its responsibilities or tasks to another object.
    • Delegation is typically achieved by defining a protocol that declares the methods that the delegate (another object) can implement.
    • In Swift, you define a delegate protocol and then have another class adopt that protocol to act as the delegate. The delegating class holds a reference to the delegate, and when a certain event occurs, it calls the appropriate delegate method.
       Purpose:
    • The delegate pattern is commonly used for handling events or responding to changes in state. For example, in UIKit, many classes use delegates to communicate user interactions, such as button taps or table view selections.
        Example:
    • In UIKit, the UITableViewDelegate protocol is used for handling events such as row selection or scrolling in a UITableView.
     protocol MyDelegate: class {
        func didSomething()
     }

     class MyClass {
         weak var delegate: MyDelegate?

         func performAction() {
             // Do something
             delegate?.didSomething()
         }
      }
2. Data Source:
Pattern: 

Data sources are implemented through protocols that define methods to supply data, such as the number of sections, the number of rows, and the content of cells. The object that wants to act as a data source adopts this protocol.

Purpose: 

Data sources are used to provide information, particularly data-related, to a component. They separate the data management and presentation logic of a UI component.

Example:

In UIKit, the UITableViewDataSource protocol is used for supplying data to UITableView, specifying the number of rows, sections, and cell content such as-

tableView:numberOfRowsInSection:: Returns the number of rows in a section.
tableView:cellForRowAtIndexPath:: Returns the cell for a row at a specified index path.
numberOfSectionsInTableView:: Returns the number of sections in the table view.
tableView:titleForHeaderInSection:: Returns the title for the header in a section.
tableView:titleForFooterInSection:: Returns the title for the footer in a section.
tableView:canEditRowAtIndexPath:: Asks if the row can be edited.

In summary, while both delegates and data sources are design patterns that involve defining protocols and using them to separate responsibilities, delegates are more about handling events and responding to changes in state, while data sources are about providing data to components like UITableView or UICollectionView.

Read More:

https://developer.apple.com

Top iOS Interview Questions and Answers

Blog Tags:Delegates and DataSource

Post navigation

Previous Post: Dictionary in Swift: Top 20 Interview Questions
Next Post: resueIdentifier in Swift

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Asked iOS Interview Questions

  • Top iOS Interview Questions and Answers

Categories

  • Associated Types(7)
  • Blog
  • Dictionary in Swift(20)
  • Initializers
  • Property Wrapper
  • Singleton in Swift
  • User Defaults(4)
  • XCode 15 Errors

Recent Comments

  1. Sid on Cycle inside MyApp; building could produce unreliable results
  2. Anominous on Cycle inside MyApp; building could produce unreliable results
  3. Aisha on @objc Attribute in Swift

Recent Posts

  • Enums in Swift: Brief Explanation with Code Examples
  • Higher-Order Functions in Swift: Brief Explanation with Code Examples
  • Mutability in Structs and Classes in Swift
  • Delegate Pattern in Swift
  • resueIdentifier in Swift

DSA in Swift

  • 2D Array in Swift: Interview Questions

Copyright © 2025 iOS Interview Questions and Tutorials.

Powered by PressBook WordPress theme