• 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: [super initialize]?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [super initialize]?


  • Subject: Re: [super initialize]?
  • From: mmalcolm crawford <email@hidden>
  • Date: Fri, 2 Sep 2005 16:36:11 -0700


On Sep 2, 2005, at 4:11 PM, Jim Correia wrote:

On Sep 2, 2005, at 6:29 PM, Scott Anguish wrote:
Oh, and this often confuses devs when dealing with KVO dependent key trigger situations..
The reason that this is particularly troublesome is that KVO registering dependent keys only triggers notifications on the class that registered. So if you implement your safe initializer
+ (void)initialize
{
if (self == [MyClass class])
{
// register depending keys
}
}
subclasses won't trigger dependent notifications.


This completely true.

Given a class, Person:

@interface Person : NSObject {
    NSString *firstName;
    NSString *lastName;
}

with dependent key 'fullName':

+ (void)initialize {
[self setKeys:[NSArray arrayWithObjects:@"firstName", @"lastName", nil]
triggerChangeNotificationsForDependentKey:@"fullName"];


}

- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]];
}


if you then implement Employee:

@interface Employee : Person {
    NSDecimalNumber *hourlyWage;
    NSDecimalNumber *hoursPerWeek;
}

then 'fullName' is still updated appropriately for instances of Employee.

If, however, you add a dependent key, 'weeklyCost', to the Employee class, and implement its initializer:

+ (void)initialize {
[self setKeys:[NSArray arrayWithObjects:@"hourlyWage", @"hoursPerWeek", nil]
triggerChangeNotificationsForDependentKey:@"weeklyCost"];
}


then KVO notifications are not sent for 'fullName' for instances of Employee.


mmalc

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: [super initialize]?
      • From: mmalcolm crawford <email@hidden>
    • Re: [super initialize]?
      • From: Jim Correia <email@hidden>
References: 
 >[super initialize]? (From: "Christopher Hickman" <email@hidden>)
 >Re: [super initialize]? (From: Scott Anguish <email@hidden>)
 >Re: [super initialize]? (From: Jim Correia <email@hidden>)

  • Prev by Date: Re: [super initialize]?
  • Next by Date: odoc appleevent not "invoking" cocoa scripting
  • Previous by thread: Re: [super initialize]?
  • Next by thread: Re: [super initialize]?
  • Index(es):
    • Date
    • Thread