Learn Swift Fundamentals by preparing for Job Interview, Questions 169 ~ 174 For KVO, KVC

In fact, by using combine or rxswift, KVO and KVC patterns are not often used, but there are cases where they are used in Apple’s framework. AVPlayer is an example. However, AVPlayer can also be used with combine. But just knowing the KVO Pattern can prove your level.

169) What is KVO? Advantages?

Answer:

  • Key Value Observing is a process of observing changes of an object.
  • KVO helps to update the UI when there is a change in the property value that is displayed in the user interface.

170) What is KVC?

Answer:

  • Key Value Coding is a process of alternate way to access Properties of a Class
class BankAccount: NSObject {

@objc dynamic var balance: Int = 0

func deductAnnumalMaintenance() {
balance -= 25
}

func depositAmount(amount: Int) {
balance += amount
}

func withdrawAmount(amount: Int) {
balance -= amount
}

}
class Human: NSObject {

@objc var myAccount: BankAccount = BankAccount()
var observer: NSKeyValueObservation?

func observeChangesInMyBalance() {
self.observer = self.observe(\\.myAccount.balance, options: [.old, .new]) { human, change in
print(change)
}
}

deinit {
observer?.invalidate()
}

}
let human: Human = Human()
human.observeChangesInMyBalance()
human.myAccount.deductAnnumalMaintenance()
// NSKeyValueObservedChange<Int>(kind: __C.NSKeyValueChange, newValue: Optional(-25), oldValue: Optional(0), indexes: nil, isPrior: false)
human.myAccount.depositAmount(amount: 300)
// NSKeyValueObservedChange<Int>(kind: __C.NSKeyValueChange, newValue: Optional(275), oldValue: Optional(-25), indexes: nil, isPrior: false)

171) Can we use KVC for Structures?

Answer:

  • NO

172) What is KVC?

Answer:

  • Key Value Coding is a process of alternate way to access Properties of a Class.
class BankAccount: NSObject {

@objc dynamic var balance: Int = 0

func deductAnnumalMaintenance() {
balance -= 25
}

func depositAmount(amount: Int) {
balance += amount
}

func withdrawAmount(amount: Int) {
balance -= amount
}

}
class Human: NSObject {

@objc var myAccount: BankAccount = BankAccount()
var observer: NSKeyValueObservation?

func observeChangesInMyBalance() {
self.observer = self.observe(\\.myAccount.balance, options: [.old, .new]) { human, change in
print(change)
}
}

deinit {
observer?.invalidate()
}

}
let human: Human = Human()
human.observeChangesInMyBalance()
human.myAccount.balance = 3000
// You can access `@objc dynamic var balance: Int` through "balance"
human.myAccount.setValue(2000, forKey: "balance")
print(human.myAccount.balance) // 2000

173) What is dynamic keyword?

Answer:

  • dynamic says to compiler that you want to use Objective-C dynamic dispatch.
  • Mainly used for KVO and for core data properties.

174) What is @objc keyword?

Answer:

  • @objc indicates that the code is available for not only swift code, but also for objc code.

--

--

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!