[MacRuby-devel] NSArrayController
Richard Kilmer
rich at infoether.com
Fri Sep 26 05:32:44 PDT 2008
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/Concepts/AccessorConventions.html#/
/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
More information about the MacRuby-devel
mailing list