Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key Value Observing with an NSArray

I've looked on SO for examples of using Key Value Observing with an NSArray (or NSMutableArray) and apparently you need to use an NSArrayController (which unlike KVO I'm not familiar with), but I haven't found concrete examples of how to do this. Can anyone explain with some sample code?

For instance, if I have a GameModel which represents its player names with an NSArray (playerNameArray) of NSStrings. I want to observe those strings (the view controller observes the model's data) to update various things in the view.

How do I get notification that the player name array has changed?

EDIT: Does the iOS SDK even support NSArrayController? If not, is there another way?

like image 672
cannyboy Avatar asked Aug 13 '10 15:08

cannyboy


People also ask

What is key-value observing?

Key-value observing is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It's useful for communicating changes between logically separated parts of your app—such as between models and views. You can only use key-value observing with classes that inherit from NSObject .

What is key-value Coding and key-value observing?

Using a mechanism called key-value coding (KVC), you can manipulate object properties indirectly. With KVC comes the ability to observe changes to a particular key value, which is known as key-value observing (KVO).

What is NSArray?

NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArrayRef . See Toll-Free Bridging for more information on toll-free bridging.

What is key-value coding?

About Key-Value Coding. Key-value coding is a mechanism enabled by the NSKeyValueCoding informal protocol that objects adopt to provide indirect access to their properties. When an object is key-value coding compliant, its properties are addressable via string parameters through a concise, uniform messaging interface.


1 Answers

You don't need an NSArrayController to observe changes to an NSArray. However you cannot directly observe these changes, i.e., you can't call -addObserver:forKeyPath:options:context: directly on an NSArray. In your case you want to call it on your GameModel with @"playerNameArray" as the key.

You're not done yet though. The normal automatic KVO notifications will only kick in if you call -setPlayerNameArray:, thereby replacing the entire array. If you want more granular notifications, then you need to use -willChange:valuesAtIndexes:forKey: and -didChange:valuesAtIndexes:forKey: whenever you insert, remove, or replace items in that array.

This will send a notification whenever the contents of the array changes. Depending on the NSKeyValueObservingOptions you use when adding your observer, you can also get the incremental changes that are made—a cool feature, but you may not need it in this case.

Note: NSArrayController does not exist on iOS. It's part of Cocoa Bindings, which currently only exists in AppKit on Mac OS X. Luckily, you don't need it.

like image 70
kperryua Avatar answered Oct 20 '22 07:10

kperryua