Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering macOS Programming

You're reading from   Mastering macOS Programming Hands-on guide to macOS Sierra Application Development

Arrow left icon
Product type Paperback
Published in May 2017
Publisher Packt
ISBN-13 9781786461698
Length 626 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Stuart Grimshaw Stuart Grimshaw
Author Profile Icon Stuart Grimshaw
Stuart Grimshaw
Gregory Casamento Gregory Casamento
Author Profile Icon Gregory Casamento
Gregory Casamento
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Hello macOS 2. Basic Swift FREE CHAPTER 3. Checking Out the Power of Xcode 4. MVC and Other Design Patterns 5. Advanced Swift 6. Cocoa Frameworks - The Backbone of Your Apps 7. Creating Views Programmatically 8. Strings and Text 9. Getting More from Interface Builder 10. Drawing on the Strength of Core Graphics 11. Core Animation 12. Handling Errors Gracefully 13. Persistent Storage 14. The Benefits of Core Data 15. Connect to the World - Networking 16. Concurrency and Asynchronous Programming 17. Understanding Xcodes Debugging Tools 18. LLDB and the Command Line 19. Deploying Third - Party Code 20. Wrapping It Up

Leveraging key-value observing


Key-value observing, generally abbreviated in literature to KVO, is a mechanism by which an object can observe properties of another object, so that when, for example, the data in your data model changes, that change is automatically reflected in the user interface. There are different ways for objects to notify one another in Cocoa; KVO, which is used for a number of purposes within Cocoa itself, including Cocoa bindings, Core Data, and AppleScript support, is a well-established pattern.

It doesn't take a huge amount of effort to set up, and does save you a fair amount of repetitive boilerplate code writing.

Coding for KVO

There are three steps involved in setting up KVO, and one cleanup step when we have finished observing:

  1. We need to prepare our classes for KVO.
  2. We need to let the system know when we wish to observe an object, and which change(s) we wish to observe.
  3. We need to override NSObject's method that handles the change notification.

And when we are done...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image