what is a valid key-path? why can't I bind to NSURL's -path method?
what is a valid key-path? why can't I bind to NSURL's -path method?
- Subject: what is a valid key-path? why can't I bind to NSURL's -path method?
- From: Perrog <email@hidden>
- Date: Sun, 18 Feb 2007 23:59:30 +0100
Hi!
I have a question about binding to a key-path where the last key path
is a method. I'm binding a view's "value" to my data model's
"currentLocation.path". CurrentLocation is a property of type NSURL,
and path is a method of NSURL (returning a portion of an URL.) I bind
because I want to show the currentLocation in a NSTextField.
But when value is bound to currentLocation.path Cocoa bails out. If I
bind value to currentLocation everything work out. I can inspect in
GDB that the bailing out depends on that Cocoa calls -[NSURL release]
recursively until the stack is exhaused. My question is how can I bind
to currentLocation.path, or can't I?
Are there any limitation what properties keypaths can designate?
The currentLocation itself is a property in sync with my WebView's
mainFrameURL via observing. If WebView's -mainFrameURL returned a
NSURL instead of NSString, I could bind directly to the WebView's
property -mainFrameURL, but this is not the case. However, I don't
think this is part of the problem.
The situation get worse because I'm not very familiar with Cocoa
Bindings. So I circumvented the problem writing a value-transformer
that simply transform an URL to an URL-path. And then everything
works. But this is removed from the source below, because I don't want
to write a value-transformer.
This is my set up:
@implementation MyDocument
- (NSURL *)currentLocation {
return _currentLocation;
}
- (void)setCurrentLocation:(NSURL *)url {
if (_currentLocation != url)
[url retain], [_currentLocation release], _currentLocation = url;
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// The NSTextField is read-only so we wont get any update from the view model.
// But MyDocument is observing WebView's mainFrameURL for changes,
// and syncs the value properly
if( [keyPath isEqualToString:@"mainFrameURL"] == YES ) {
NSString *URLString = [change objectForKey:NSKeyValueChangeNewKey];
[self willChangeValueForKey:@"currentLocation"];
[self setCurrentLocation:[NSURL URLWithString:URLString]];
[self didChangeValueForKey:@"currentLocation"];
}
}
@end
//------------------------------------------------------------------------------------------
// This is my controller. The controller programmatically
// binds the bindings, because we cannot do it in Interface Builder.
@implementation Controller
- (void)windowDidLoad {
// bind document's location to the webView's mainFrameURL
[_webView addObserver:[self document]
forKeyPath:@"mainFrameURL"
options:NSKeyValueObservingOptionNew
context:NULL];
// Bind the location-field value to the document's currentLocation property
[_locationField bind:@"value" toObject:[self document]
withKeyPath:@"currentLocation.path"
options:NULL];
}
@end
My question is how can I bind to currentLocation.path, or can't I?
Are there any limitation what properties keypaths can point at?
Thanks for any hints!
Rog
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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