Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Key-Value-Observing and Collections



Philip,

You can expose 'grades' as a to-many property by adding the following methods to Pupil

- (unsigned int) countOfGrades
- (id) objectInGradesAtIndex:(unsigned int)index
- (void) insertObjectInGrades:(id)grade atIndex:(unsigned int)index
- (void) removeObjectFromGradesAtIndex:(unsigned int)index
- (void) replaceObjectInGradesAtIndex:(unsigned int)index withObject: (id)grade


Implement these to manipulate your instance variable (which you must rename to something other than grades).
Now all methods of NSMutableArray are applicable to your grades property and will automatically generate
the appropriate key/value observations. For example


  NSMutableArray *myGrades = [me mutableArrayValueForKey:@"grades"];
  [myGrades removeObject:badGrade];
  [myGrades addObject:goodGrade];

You might add the following method for aesthetics...

- (NSMutableArray *) mutableGrades
  { return [self mutableArrayValueForKey:@"grades"]; }

 dave

On 25-Oct-07, at 7:23 AM, Philip Mötteli wrote:

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 collection (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


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please 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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >Key-Value-Observing and Collections (From: Philip Mötteli <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.