• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
what is a valid key-path? why can't I bind to NSURL's -path method?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: what is a valid key-path? why can't I bind to NSURL's -path method?
      • From: mmalc crawford <email@hidden>
    • Re: what is a valid key-path? why can't I bind to NSURL's -path method?
      • From: Mike Abdullah <email@hidden>
  • Prev by Date: NSTokenField & Space Bar selection
  • Next by Date: Re: what is a valid key-path? why can't I bind to NSURL's -path method?
  • Previous by thread: Re: How do you close a NSDrawer after a NSThread has finished
  • Next by thread: Re: what is a valid key-path? why can't I bind to NSURL's -path method?
  • Index(es):
    • Date
    • Thread