• 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
Re: Key-Value-Observing and Collections
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Key-Value-Observing and Collections


  • Subject: Re: Key-Value-Observing and Collections
  • From: Philip Mötteli <email@hidden>
  • Date: Thu, 25 Oct 2007 16:21:41 +0200

Am 25.10.2007 um 15:51 schrieb David Spooner:

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"]; }

Thanks for these really interesting points!
What would you do, if you just want to be notified, when an object has been added or removed from some NSMutableArray?



Thanks Phil



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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Key-Value-Observing and Collections
      • From: Mike Abdullah <email@hidden>
References: 
 >Key-Value-Observing and Collections (From: Philip Mötteli <email@hidden>)
 >Re: Key-Value-Observing and Collections (From: David Spooner <email@hidden>)

  • Prev by Date: Re: Key-Value-Observing and Collections
  • Next by Date: Re: Troublesome app when scaling it up, how to proceed?
  • Previous by thread: Re: Key-Value-Observing and Collections
  • Next by thread: Re: Key-Value-Observing and Collections
  • Index(es):
    • Date
    • Thread