Sobes.tech
Junior

What methods of data transfer do you know?

sobes.tech AI

Answer from AI

  • Properties and Initializers: Passing data through properties and initializers when creating an object.
  • Segues (for Storyboards): Passing data between View Controllers during navigation using prepare(for:sender:).
  • Delegation: Implementing a protocol that allows one object (delegate) to receive notifications and data from another object.
  • NotificationCenter: A Publish-Subscribe mechanism for broadcasting notifications with arbitrary data.
  • Key-Value Observing (KVO): A mechanism for observing changes to object properties.
  • Closures/Callbacks: Passing data through closures that execute asynchronously or after a specific event.
  • Singletons: A shared instance of a class accessible from anywhere in the app, for storing and exchanging data.
  • User Defaults: Storing small amounts of user data in persistent storage.
  • Core Data/Realm/SQLite: Storage and management of structured data on the device.
  • File Storage: Saving data to files on disk (e.g., documents, images).
  • CloudKit: Cloud service for storing and synchronizing data across devices via iCloud.
  • Networking (APIs): Transferring data between the app and server over the network.
  • Environment Objects (SwiftUI): Providing objects shared across the view hierarchy.
  • State Objects (SwiftUI): Managing local view state.
  • Observed Objects (SwiftUI): Managing objects that the view subscribes to.

Example of using Segues:

// In the source View Controller
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {
        if let destinationVC = segue.destination as? DetailViewController {
            destinationVC.dataToReceive = "Hello from source VC"
        }
    }
}

Example of using Delegation:

// Delegate protocol
protocol DataDelegate: AnyObject {
    func didReceiveData(_ data: String)
}

// Class that sends data
class SendingViewController: UIViewController {
    weak var delegate: DataDelegate?

    func sendData() {
        delegate?.didReceiveData("Data sent via delegate")
    }
}

// Class that receives data
class ReceivingViewController: UIViewController, DataDelegate {
    func didReceiveData(_ data: String) {
        print("Received data: \(data)")
    }
}

Example of using NotificationCenter:

// Sending a notification
NotificationCenter.default.post(name: Notification.Name("myDataNotification"), object: nil, userInfo: ["dataKey": "Data from NotificationCenter"])

// Receiving a notification
NotificationCenter.default.addObserver(self, selector: #selector(handleDataNotification(_:)), name: Notification.Name("myDataNotification"), object: nil)

@objc func handleDataNotification(_ notification: Notification) {
    if let receivedData = notification.userInfo?["dataKey"] as? String {
        print("Received data: \(receivedData)")
    }
}

// Don't forget to remove observer
deinit {
    NotificationCenter.default.removeObserver(self)
}