Key-Value-Observing and Collections

Christiaan Hofman cmhofman at gmail.com
Thu Oct 25 07:47:21 PDT 2007


You should avoid that, in particular for collection keys (for those  
there are other variants that allow you to pass precisely what has  
changed). A much better and easier solution is to follow the docs:  
use KVC compliant array accessors, like  
insertObject:inGradesAtIndex:, which automatically send the correct  
notifications. If you really want to use addGrade:, just make it a  
wrapper around this KVC compliant method.

Christiaan


On 25 Oct 2007, at 4:35 PM, Fabian Schuiki wrote:

> Hi,
>
> Call [self willChangeValueForKey:@"grades"] at the beginning of - 
> (void)addGrade: and [self didChangeValueForKey:@"grades"] just when  
> you changed the grades variable.
>
> Best,
> Fabian
>
> Fabian Schuiki
> fabianschuiki at bluewin.ch
>
>
> Am 25.10.2007 um 15:23 schrieb Philip Mötteli:
>
>> Hi,
>>
>>
>> There is something, I just don't get with KVO. Lets say, I have an  
>> object
>>
>>
>> @interface Pupil
>> {
>> 	NSMutableArray	grades;
>> }
>>
>>
>> - (void)setGrades:(NSMutableArray *)theGrades;
>> - (NSMutableArray *)grades;
>> - (void)addGrade:(Grade *)aGrade;
>>
>> @end
>>
>>
>> Using KVO, I want to be notified not only, when the old grades  
>> collection object (NSMutableArray) is replaced, but also, when a  
>> Grade instance is added to the grades array (something like  
>> "[grades addObject:aGrade]").
>> The first case is easy, because I know the attribute name  
>> (grades), which gives the key. But for the second, I don't know  
>> the attribute name, so I can't add an observer.
>> Can anybody explain me how to do this?
>>
>>
>> Thanks
>> Phil
>>
>> _______________________________________________
>> MacOSX-dev mailing list
>> MacOSX-dev at omnigroup.com
>> http://www.omnigroup.com/mailman/listinfo/macosx-dev
>
> _______________________________________________
> MacOSX-dev mailing list
> MacOSX-dev at omnigroup.com
> http://www.omnigroup.com/mailman/listinfo/macosx-dev



More information about the MacOSX-dev mailing list