changeKeyPath:ofObject:toValue:


#1

Hi,
this may be a silly question, but on page 154, it says:
"Now, implement the method that does edits and is its own reverse: …"
But that method, namely changeKeyPath:ofObject:toValue:, does only edit anything, when undo or redo is used, correct? It does not edit anything when a new employee is added or you edit an existing one.

Cheers
Phil


#2

The method, changeKeyPath:ofObject:toValue, will be called when you undo an edit for personName or for expectedRaise.
And it change the value of specified keypath of the specified object.
so, if you edit expectedRaise value from 5% to 10% for a person and undo the edit, changeKeyPath:ofObject:toValue will be called with keyPath=expectedRaise, obj=a Person instance, and newValue=5%.
it is editing the expectedRaise back to the oldValue.

Adding NSLog inside the method helped me what it is doing.

[code]-(void)changeKeyPath:(NSString*)keyPath ofObject:(id)obj toValue:(id)newValue{
NSLog(@“changeKeyPath is called. keyPath= %@, obj= %@, newValue= %@”, keyPath, obj, newValue);

[obj setValue:newValue forKeyPath:keyPath];

}[/code]

I think the method name of this example is not good and make it difficult to understand what’s going on. it is easily misunderstood as if changing KeyPath itself.
changeValue:ofObject:forKeyPath” or “setValue:ofObject:forKeyPath” will be better