Build an iOS Application Using the Key-Value-Observer Pattern (KVO) | by Özgün Yildiz | Apr, 2022

Methods to use dynamic dispatches in your codebase

Key-value observing is a Cocoa programming sample you employ to inform objects about adjustments to properties of different objects.

Supply: Apple Documentation

Ever watched the Truman Present? It’s a couple of man residing an extraordinary life whereas his each transfer is recorded and broadcast to an enormous viewers.

OK, however why am I speaking about motion pictures? Effectively, the Truman Present has some similarities to the Key Observer Sample. Think about the protagonist of the Truman Present, Truman Burbank, to be a property of a category inside your app that you just need to observe for adjustments.

In an effort to obtain that, we now have to make use of one thing referred to as dynamic dispatch, which I’ll clarify later (analogous to the Truman Present, you may consider this step involving the set-up of cameras and TV gear).

AnimeFight App

What we’re going to construct

Obtain the starter undertaking here.

Now we have two View Controllers referred to as KVOBaseScreen and KVOSelectionScreen. Based mostly on the button pressed on the KVOSelectionScreen, the picture, label, and the colour of the KVOBaseScreen change.

Consider the analogy I proposed earlier; We need to observe a property for adjustments. It is smart to create an object wherein we retailer these UI components — we will merely observe the mannequin for adjustments.

It additionally is smart to create a separate service class for that as a result of we will:

  1. Create a perform contained in the service class that’s being referred to as each time one of many two buttons within the app is pressed.
  2. Create a variable wherein the brand new occasion that was handed with the perform is saved. With a singleton, we will entry the properties of the KVOService class. Now the one factor that’s left to do right here is to watch the kvoModel for adjustments.

The Key-Worth Observer sample is Goal-C primarily based, which is why each lessons should be marked with the objc key phrase. The explanation why we don’t use structs is that with KVO, we will’t use worth sorts, so no structs, enums or tuples. Worth sorts are usually not representable in Goal-C runtime. We mark the variable we need to observe for adjustments with @ objc dynamic var. The dynamic key phrase is what bridges Goal-C into the Swift world, so it runs in Goal-C runtime.

On this step, we observe adjustments with the inbuilt observe technique. It accepts three arguments and it seems to be like this:

_ keypath: — is the trail to the property we need to observe. In our case, the property we need to observe is in our KVOService and with a view to entry it, we use a singleton. Our goal is the kvoModel variable.

choices: — with this, we will determine whether or not we would like entry to the outdated worth, the brand new worth, or each. The brand new worth is the property put up change, the outdated worth is the property prior change. In some apps you would possibly need to make use of the outdated property, however in our app we don’t have any use for it.

changeHandler: — is a closure that accepts two arguments and returns Void. With the primary argument, we will entry the property we’re observing. The second argument is of kind NSKeyValueObservedChange and denotes our property put up the change. We are able to entry it with .newValue . We use the newValue by feeding it to our setView perform, with which we execute UI adjustments.

The observe technique returns a component of kind NSKeyValueObservation. That is the statement of our property, which we now have to retailer someplace. We observe it in a variable of kind Any. We need to keep away from a retain cycle, so we seize self with weak.

The one factor left to do, is to mutate the property itself (analogously, for Mr. Burbank to really go and dwell his life).

Each time certainly one of these two buttons is pressed, sideChosen will likely be referred to as and mutate the our kvoModel that’s being noticed in our KVOSeriveClass.

Hope you discovered one thing. The total undertaking may be discovered here.

Wish to Join?You probably have any questions, contact me on Twitter.

More Posts