NSBrief

#8: Jiva Devoe


Listen Later

Saul and Jiva talk about the cross platform architecture of the new todo list app, Wasabi, from Random Ideas Software. We also get to discussions on MVC, Blocks and code organization.

Download Episode

Show Links

Random Ideas Website

Wasabi Blog Post Announcement

Wasabi on the App Store

Frameworks are teh suck, err, By Wil Shipley - http://www.wilshipley.com/blog/2005/11/frameworks-are-teh-suck-err.html

Events

NSCoderNight

Denver NSCoder, Tuesdays, 6pm at Common Grounds Coffee, Downtown Denver.
Follow @jwilker on twitter for up to date information.

iOS/Cocoaheads Meetups

Denver iPhone User Group
Tuesday Feb 15, 6:30pm at Remy 1660 17th St, Downtown Denver.

CocoaHeads Colorado Springs
Thursday March 10, 7pm at Panera Bread, 7344 North Academy Blvd. Colorado Springs, CO

CocoaHeads Boulder
Tuesday March 8, 7pm at Inspiring Apps, 1045 Pearl Street Boulder, CO

Colorado iPhone Developers Meetup
No Meeting Currently Scheduled

Gettin’ to Know Cocoa

Welcome to Gettin’ to Know Cocoa, where we crack open a piece of Cocoa documentation and hopefully let you know about another useful piece of the Cocoa framework.

Today on Gettin’ to Know Cocoa, we’re going to take a look at NSKeyValueCoding and NSKeyValueObserving. If you’re new to Cocoa, you should know that KVC an KVO are integral parts of the Cocoa framework. More often than not, your code can gain a lot of benefits in flexibility  . Both of these are defined as information protocols in Cocoa. Informal protocols contain a collection of method declarations whereby you can optionally implement the methods to change behavior. Let’s start with NSKeyValueCoding…

If you’ve used Key-Value coding in your cocoa apps, you may expect the usual suspects, such as valueForKey:, setValue:forKey:, valueForKeyPath: and setValue:forKeyPath:. Some overlooked methods are dictionaryWithValuesForKeys:, valueForUndefinedKey: and valueValue:forKey:error:.

Let’s take those one at a time.

dictionaryWithValuesForKeys:, and it’s corresponding mutator, setValuesForKeysWithDictionary: allow you to set and retrieve a subset of key-value pairs in your dictionaries in a single method call. The really neat trick with this method is that since NSObjects conform to this informal protocol, you can set and retrieve a subset of your objects property values with a single method call. setValuesForKeysWithDictionary: and dictionaryWithValuesForKeys both work with an instance’s valueForKey: and setValueForKey: method, so there isn’t as much magic as you may expect. But it’s nice to know that this handy shortcut is available for your use in both your Mac and iOS apps.

valudateValue:forKey:error: does what it sounds like, validates the value of the provided key. Since you most likely won’t be calling this method directly, you want to follow the guidelines for this method closely. First, you want to return the proper value whether a property is valid or not. You could, however, always return YES, and correct the value this method. So, it may also be the case that you want to validate more than one value in your class or collection. In this case, you should probably leave this method as the default and instead implement validate:error:, where is the name of the property you need to validate. Working with validations like this will rid your code of those unsightly if/else checks. The rules of this method are the same, you want to return YES if the property is valid, or if you’ve corrected it within the validation, and NO if it isn’t. A note on the error parameter, it is your responsibility to return an autoreleased error object, as it is not the sender’s responsibility to release the error. And, yes, there is a validateValue:forKeyPath:error method

valueForUndefinedKey: is invoked by valueForKey: when it is unable to, surprise, find a value for the given key. This method is called prior to giving up to give you one last chance to handle this specific problem yourself, if you need to do so. The default value of this method raises an NSUndefinedKeyException, which is why your apps seem to crash if you query for an undefined key. The way you use this method is to implement it in your subclasses of NSObject.

Moving on to the NSKeyValueObserving protocol, I found it funny that the docs spelled out that KVO is not available for Java applications. Let that be a lesson, don’t write Cocoa apps in Java.  Again, if you’ve done any KeyValue Coding, you’re most likely aware of methods such as addObserver:forKeyPath:options:context:, removeObserver:forKeyPath: and observeValueForKey:ofObject:change:context:. These three methods are at the heart of implementing KVO. Other methods in this protocol include didChangeValueForKey:withSetMutation:usingObjects:, didChange:valuesAtIndexes:forKey:, and the corresponding willChange methods. Remember, if you override a synthesized implementation of a property in your class, and you want to observe value changes, you must call the proper willChange and didChange method before and after you’ve change the value of the property. These two variations are to perform notifications in two specific situations. The first, did/willChangeValueForKey:withSetMutation:usingObjects: is when your property has an unordered relation. And the did/willChange:valuesAtIndexes:forKey: are when you have an ordered relationship. These methods must be used by your property accessor and mutator methods if you want them to maintain KVO compliance.

Again, these protocols are defined on both iOS and Mac since they reside in the Foundation.framework. On the mac, KVC and KVO is especially key since this is the heart of how bindings work…

That’s all we have for today, I’m Saul Mora, founding Panda at Magical Panda Software, and engineer at Double Encore, helping you to get to know Cocoa.

...more
View all episodesView all episodes
Download on the App Store

NSBriefBy Saul Mora