Protocols.swift
import Foundation
protocol PresenterOutput: AnyObject {
func displayData(_ data: String)
}
protocol PresenterInput {
var view: PresenterOutput? { get set }
func updateView()
}
protocol ModelInput {
func fetchData() -> String
}
Model: DataModel.swift
import Foundation
class DataModel: ModelInput {
func fetchData() -> String {
// Fetch and return data
return "Sample Data"
}
}
View: DataViewController.swift
import UIKit
class DataViewController: UIViewController, PresenterOutput {
var presenter: PresenterInput!
override func viewDidLoad() {
super.viewDidLoad()
// Initialize the presenter with the model
presenter = DataPresenter(model: DataModel())
presenter.view = self
// Update view via presenter
presenter.updateView()
}
func displayData(_ data: String) {
// Update your UI with the data
// For example, display the data in a label
print(data)
}
}
Presenter: DataPresenter.swift
import Foundation
class DataPresenter: PresenterInput {
weak var view: PresenterOutput?
var model: ModelInput
init(model: ModelInput) {
self.model = model
}
func updateView() {
let data = model.fetchData()
view?.displayData(data)
}
}