Re: Binding question
Re: Binding question
- Subject: Re: Binding question
- From: Quincey Morris <email@hidden>
- Date: Tue, 28 Oct 2008 12:58:20 -0700
On Oct 28, 2008, at 12:30, Jean-Nicolas Jolivet wrote:
One more thing, you mentioned the term "KVO Compliance"... I
understand that this means to send the proper notifications when a
change to my property has been made...
But: would it be considered bad practice if my file class does have
a property that is used just for displaying purpose (For example, by
combining other properties (fileName, extension, size) into a nicely
formatted string) to display in a tableView... by bad practice I
mean: I know that this property is only used for display purpose and
will never be edited... but will be modified when other properties
(filePath etc..) have been modified?
No, it's not bad practice to have a "derived" property for display
purposes. That's basically what -[NSObject description] is, for
example, and that's a *really* important property.
Would it mean that this property is not "KVO compliant" and if so,
is it a problem? Assuming I know it should never be "observed" (i.e.
it will never be modified directly, only by modifying other
properties)...
Not sure what this means. "Observed" doesn't mean modifiable. It means
some other object is watching for change notifications. In particular,
if you bind something to your derived property (which is what you're
doing in your table view), the property gets observed by the binding,
and so must be KVO compliant.
basically from your post I understand that.. technically, it's not a
problem to do it like that, but... would it be considered bad
practice since the property is not KVO compliant? and if so, is it
even possible to make it KVO compliant?
There's at least 2 ways. If you have a setter for "filePath", you can
do this:
- (void) setFilePath: (NSString*) newValue {
[self willChangeValueForKey: @"fileName"];
filePath = newValue; // plus retain/release code too, if you aren't
using garbage collection
[self didChangeValueForKey: @"fileName"];
}
Or you can have it happen automatically:
@implementation File
+ (NSSet*) keyPathsForValuesAffectingFileName {
return [NSSet set withObject: @"filePath"];
}
...
(The latter is the Leopard way. For Tiger, you use
'setKeys:triggerChangeNotificationsForDependentKey:' instead.)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden