Hi, Is it possible to use bind values using macruby? I've been working through Hillegass' Cocoa Programming For Mac OS X, converting the exercises to MacRuby. I've hit a big of a snag in the RaiseMan exercise though. It uses an NSArrayController as a binding for a table view. There is then a NSDocument called Person which is bound to provide the values for the table. When I try to run this, I'm getting an error: this class is not key value coding-compliant for the key employees I've tried a number of different ways to make employees respond: attr_accessor :employees and also using a manual setter (in two different ways): def employee=(val) end def setEmployee(val) end Neither seem to work. Am I doing something wrong? Is this unimplemented? I've looked at a RubyCocoa implementation of this exercise, and they just completely skip the NSArrayController and just set up a data source. Thanks for any help. Brad
Hi Brad, On Sep 25, 2008, at 6:29 PM, Brad Wilson wrote:
Hi,
Is it possible to use bind values using macruby?
I've been working through Hillegass' Cocoa Programming For Mac OS X, converting the exercises to MacRuby.
I've hit a big of a snag in the RaiseMan exercise though. It uses an NSArrayController as a binding for a table view. There is then a NSDocument called Person which is bound to provide the values for the table.
When I try to run this, I'm getting an error: this class is not key value coding-compliant for the key employees
I've tried a number of different ways to make employees respond: attr_accessor :employees and also using a manual setter (in two different ways): def employee=(val) end def setEmployee(val) end
Neither seem to work. Am I doing something wrong? Is this unimplemented?
I never really tried to use NSArrayController in MacRuby, but I know that Rich uses it in HotCocoa and it seems to work well. And it's certainly possible to use bindings in MacRuby. Could you upload your code somewhere so that we can have a look at it? Also, it's great that you're converting the book exercises to MacRuby. If you are willing to contribute them after, I would be glad to package them in the distribution :-) Laurent
On Sep 25, 2008, at 10:40 PM, Laurent Sansonetti wrote:
Hi Brad,
On Sep 25, 2008, at 6:29 PM, Brad Wilson wrote:
Hi,
Is it possible to use bind values using macruby?
I've been working through Hillegass' Cocoa Programming For Mac OS X, converting the exercises to MacRuby.
I've hit a big of a snag in the RaiseMan exercise though. It uses an NSArrayController as a binding for a table view. There is then a NSDocument called Person which is bound to provide the values for the table.
When I try to run this, I'm getting an error: this class is not key value coding-compliant for the key employees
I've tried a number of different ways to make employees respond: attr_accessor :employees and also using a manual setter (in two different ways): def employee=(val) end def setEmployee(val) end
Neither seem to work. Am I doing something wrong? Is this unimplemented?
I never really tried to use NSArrayController in MacRuby, but I know that Rich uses it in HotCocoa and it seems to work well. And it's certainly possible to use bindings in MacRuby.
Here's an example of a key binding for an item that is an NSCollectionViewItem (thus representedObject) image.bind "value", toObject:item, withKeyPath:"representedObject.user.profile_image", options:nil So I am binding the value of the image to the item object's keypath: representedObject.user.profile_image It looks like its complaining that the array of objects that you are placing in ArrayController is causing and exception that it is not KVC compliant? As Laurent indicated, if you post what you have we might be able to help. Best, Rich
Could you upload your code somewhere so that we can have a look at it?
Also, it's great that you're converting the book exercises to MacRuby. If you are willing to contribute them after, I would be glad to package them in the distribution :-)
Laurent _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Here's an example of a key binding for an item that is an NSCollectionViewItem (thus representedObject)
image.bind "value", toObject:item, withKeyPath:"representedObject.user.profile_image", options:nil
So I am binding the value of the image to the item object's keypath: representedObject.user.profile_image
It looks like its complaining that the array of objects that you are placing in ArrayController is causing and exception that it is not KVC compliant?
I guess that's the crux of my question - how do I make an object KVC compliant? Like I said, I tried attr_accessor employes, etc but didn't seem to have any luck. Thanks for the help. I'll try to get the code in a state to demonstrate my problem and send it up. Laurent - I'd be happy to send through any exercises. I'll send through a collection once I get some more done. Brad
As Laurent indicated, if you post what you have we might be able to help.
Best,
Rich
Could you upload your code somewhere so that we can have a look at it?
Also, it's great that you're converting the book exercises to MacRuby. If you are willing to contribute them after, I would be glad to package them in the distribution :-)
Laurent _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
_______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On Sep 26, 2008, at 3:17 AM, Brad Wilson wrote:
Here's an example of a key binding for an item that is an NSCollectionViewItem (thus representedObject)
image.bind "value", toObject:item, withKeyPath:"representedObject.user.profile_image", options:nil
So I am binding the value of the image to the item object's keypath: representedObject.user.profile_image
It looks like its complaining that the array of objects that you are placing in ArrayController is causing and exception that it is not KVC compliant?
I guess that's the crux of my question - how do I make an object KVC compliant? Like I said, I tried attr_accessor employes, etc but didn't seem to have any luck.
Thanks for the help. I'll try to get the code in a state to demonstrate my problem and send it up.
Laurent - I'd be happy to send through any exercises. I'll send through a collection once I get some more done.
Brad <snip>
Brad, Try this: def initialize @employees = [] end # for reading def countOfEmployees @employees.length end def objectInEmployeesAtIndex(i) @employees[i] end # for writing def insertObject(object, inEmployeesAtIndex:i) @employees[i,0] = object end def removeObjectFromEmployeesAtIndex(i) @employees.delete_at(i) end # you can also implement: def replaceObjectInEmployeesAtIndex(i, withObject:object) @employees[i, 1] = object end There is an additional reader method but you are supposed to pass in a buffer and I don't think we can do that: getEmployees(buffer, range:range) I found this in the Indexed Accessor Patters for To-Map Properties section of the KVC programming guide: http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Con... /apple_ref/doc/uid/20002174-BAJEAIEE Hope that helps! Laurent: I wonder if we could generically add methods to ruby's Array to make it KVC compliant? countOfObjects objectInObjectsAtIndex(i) insertObject(object, inObjectsAtIndex:i) removeObjectFromObjectsAtIndex(i) (and the two optional performance enhancing methods...replaceObjectInObjectsAtIndex(i withObject:object) and getObjects(objectsBuffer?, range:NSRange_instance)) Then we could have an Array generically be KVC compliant using the path: "objects" Just a thought! Best, Rich
On 26/09/2008, at 10:32 PM, Richard Kilmer wrote:
On Sep 26, 2008, at 3:17 AM, Brad Wilson wrote:
Brad,
Try this:
def initialize @employees = [] end
I found this in the Indexed Accessor Patters for To-Map Properties section of the KVC programming guide:
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Con... /apple_ref/doc/uid/20002174-BAJEAIEE
Ok that makes sense. I'll try it out. Thanks! I don't entirely understand KVC yet, and from what I understood the standard ruby attr_accessor would be enough to handle it. I'll try adding the extra methods. Brad
On Sep 26, 2008, at 8:52 AM, Brad Wilson wrote:
On 26/09/2008, at 10:32 PM, Richard Kilmer wrote:
On Sep 26, 2008, at 3:17 AM, Brad Wilson wrote:
Brad,
Try this:
def initialize @employees = [] end
I found this in the Indexed Accessor Patters for To-Map Properties section of the KVC programming guide:
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Con... /apple_ref/doc/uid/20002174-BAJEAIEE
Ok that makes sense. I'll try it out. Thanks!
I don't entirely understand KVC yet, and from what I understood the standard ruby attr_accessor would be enough to handle it. I'll try adding the extra methods.
I think it depends on the way in which the key is being accessed. Sounds like the key you are accessing is being accessed as a collection (thus the need for these methods). If it was a simple property: attr_accessor :employees Would have been sufficient -rich
Brad _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On 26/09/2008, at 11:03 PM, Richard Kilmer wrote:
On Sep 26, 2008, at 8:52 AM, Brad Wilson wrote:
On 26/09/2008, at 10:32 PM, Richard Kilmer wrote:
On Sep 26, 2008, at 3:17 AM, Brad Wilson wrote:
Brad,
Try this:
def initialize @employees = [] end
I found this in the Indexed Accessor Patters for To-Map Properties section of the KVC programming guide:
http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Con... /apple_ref/doc/uid/20002174-BAJEAIEE
Ok that makes sense. I'll try it out. Thanks!
I don't entirely understand KVC yet, and from what I understood the standard ruby attr_accessor would be enough to handle it. I'll try adding the extra methods.
I think it depends on the way in which the key is being accessed. Sounds like the key you are accessing is being accessed as a collection (thus the need for these methods).
If it was a simple property:
attr_accessor :employees
Would have been sufficient
-rich
I've spent some more time on this and it looks like I don't need those extra methods you mentioned (though it's good to know they exist because it sure helped with debugging. I'm not sure why this is, but it seems like the initialize method isn't getting called for MyDocument ( which extends NSDocument). I've replaced that with the code below and it seems to work fine. Which is good (I guess). def init @employees = [] super end Anyway, thanks again for your help. Brad
I don't entirely understand KVC yet, and from what I understood the standard ruby attr_accessor would be enough to handle it. I'll try adding the extra methods.
I think it depends on the way in which the key is being accessed. Sounds like the key you are accessing is being accessed as a collection (thus the need for these methods).
If it was a simple property:
attr_accessor :employees
Would have been sufficient
As Brad pointed out, you don’t need all the extra methods. The downside of the single accessor is that the original collection for the property will be thrown out every time, which can cause a bit of churn if you do anything with the data in your setter (subscribe to KVO notifications for each individual object, for instance). The extra methods simply let you provide more fine-grained access to your backing store to the KVC machinery. -Ben
participants (4)
-
Benjamin Stiglitz
-
Brad Wilson
-
Laurent Sansonetti
-
Richard Kilmer