How to return a value from Model to Controller using the [Swift5] protocol

Introduction

This article is a sequel to the previously posted ** [Swift5] How to communicate from a view controller to a Model and pass a value **. If you haven't seen it yet, I think it's better to see here ▼ first. https://qiita.com/nkekisasa222/items/dabe806c23d3890a009f

Let's get into the main subject!

Creating a protocol

First, write the code.

SampleModel.swift



//① Create a protocol here
protocol DoneCatchProtocol {
    
    //(2) Specify the value and type you want to return to Controller (String this time)
    func catchSampleData(sampleValueA: String, sampleValueB: String, sampleValueC: String)
}

class SampleModel {

    var sampleValueA: String?
    var sampleValueB: String?
    var sampleValueC: String?

    //③ Create an instance of the protocol
    var doneCatchProtocol: DoneCatchProtocol?

    init(firstSampleValue: String, secondSampleValue: String, thirdSampleValue: String) {

        sampleValueA = firstSampleValue
        sampleValueB = secondSampleValue
        sampleValueC = thirdSampleValue
    }

    //④ Create a method because processing is performed in Model
    func processingSampleModel {
    
        //⑤ Call the protocol created here and enter the value
        self.doneCatchProtocol?.catchSampleData(sampleValueA: sampleValueA, sampleValueB: sampleValueB, sampleValueC: sampleValueC)
    }

I will summarize the flow.

(1) Create a protocol on the Model side.
(2) Specify the key value and type of the value you want to return.
③ Create an instance of the protocol.
④ Create a method because processing is performed in Model
(5) Call the protocol and specify the value you want to call in Controller.

Description on the Controller side

First, write the code.

SampleViewController.swift



//(1) Call the Controller so that it can handle the protocol.
class SampleViewController: UIViewController, DoneCatchProtocol {

    firstSampleValue  = "firstSampleValue"
    secondSampleValue = "secondSampleValue"
    thirdSampleValue  = "thirdSampleValue"

    //④ Property to substitute the value returned from SampleModel
    var valueA: String?
    var valueB: String?
    var valueC: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        startSampleModel()
    }

    func startSampleModel() {

      let sampleModel = SampleModel(firstSampleValue: firstSampleValue, secondSampleValue: secondSampleValue, thirdSampleValue: thirdSampleValue)

      //③ Entrust to SampleModel protocol and call method
      sampleModel.doneCatchProtocol = self
      sampleModel.processingSampleMode()
    }

    //(2) It is automatically generated when the protocol is called on the Controller side.
    func catchSampleData(sampleValueA: String, sampleValueB: String, sampleValueC: String) {

        //⑤ Substitute the value returned from SampleModel
        valueA = sampleValueA
        valueB = sampleValueB
        valueC = sampleValueC
    }
}

I will summarize the flow.

(1) Call the Controller so that it can handle the protocol.
(2) When the protocol is called in (1), the method is automatically generated.
③ Entrust to SampleModel protocol and call method
④ Prepare a property to substitute the value returned from SampleModel
⑤ Substitute the value returned from SampleModel

Now you can use the value processed by Model in Controller.

Finally

In the previous article and this article, I posted about how to communicate from Controller to Model, return the contents processed by Model to Controller, and handle it in Controller. Isn't it a development method that is often handled in the MVC model? I think that you can refer to it by all means.

Thank you for watching until the end!

Recommended Posts

How to return a value from Model to Controller using the [Swift5] protocol
[Swift5] How to communicate from ViewController to Model and pass a value
How to delete a controller etc. using a command
[Ruby] How to use is_a? Differences from kind_of? and instance_of ?. How to check the type and return a boolean value.
How to delete custom Adapter elements using a custom model
9 Corresponds to the return value
How to transition from the [Swift5] app to the iPhone settings screen
How to create a form to select a date from the calendar
[Swift] How to send a notification
How to create a jar file or war file using the jar command
[Swift] How to implement the Twitter login function using Firebase UI ①
How to run a Kotlin Coroutine sample from the command line
[Swift] How to implement the Twitter login function using Firebase UI ②
[Controller] I want to retrieve the numerical value of a specific column from the DB (my memo)
How to execute a contract using web3j
How to sort a List using Comparator
[Swift] How to use Tab Bar Controller
How to call Swift 5.3 code from Objective-C
[Swift] How to implement the countdown function
How to pass the value to another screen
[Swift5] How to create a splash screen
[Swift5] How to implement animation using "lottie-ios"
[SpringBoot] How to write a controller test
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
How to run a GIF file from the Linux command line (Ubuntu)
[Java] How to get to the front of a specific string using the String class
How to get the setting value (property value) from the database in Spring Framework
How to change the value of a variable at a breakpoint in intelliJ
[Swift] How to set an image in the background without using UIImageView.
How to create a route directly from the URL you want to specify + α
[Rails] How to create a graph using lazy_high_charts
How to get a heapdump from a Docker container
How to get the ID of a user authenticated with Firebase in Swift
[Swift] How to implement the LINE login function
[Swift] How to generate an ID to uniquely identify a certain thing (using UUID)
[swift5] How to transition from the app to an external site by specifying the URL
[swift5] How to implement the Twitter share function
[Ethereum] How to execute a contract using web3j-Part 2-
How to add sound in the app (swift)
How to implement the breadcrumb function using gretel
[Swift] How to link the app with Firebase
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
Transition to a view controller with Swift WebKit
How to generate a primary key using @GeneratedValue
[Swift] How to implement the fade-in / out function
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
When inserting from Java to MySQL, get Auto_Increment_ID (automatic numbering value) as the return value
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Pass arguments to the method and receive the result of the operation as a return value
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
3. Create a database to access from the web module
How to make a factory with a model with polymorphic association
Output the maximum value from the array using Java standard output
[Swift] How to play songs from your music library
How to run the SpringBoot app as a service
How to return to the previous screen by Swipe operation
How to jump from Eclipse Java to a SQL file
How to write Scala from the perspective of Java
How to deploy to Heroku from a local docker image
[Java] How to extract the file name from the path
[Java] How to get the maximum value of HashMap