FROM : glenn andreas
DATE : Fri Nov 23 19:29:44 2007
On Nov 23, 2007, at 12:09 PM, Philip Mötteli wrote:
>> KVO is not based on the internals of an object (which is assumed to
>> be hidden), and it does not access hiddden internals. KVO is based
>> on KVC and uses accessors.
>
> In order to use KVO, you need to know this famous "key". The key is
> the name of an instance variable, which is an object's internal
> data. Every OOP book will tell you that (<Object-Oriented
> Programming with Objective-C- Classes.webloc>):
No you don't. All that matters is that there is a method "foo" that
returns something, and a corresponding "setFoo:" that takes a similar
type as "foo" returns and you can observe it (and the second method
isn't needed, so long as some mechanism exists to let KVO know when
the value of "foo" has changed)
It doesn't matter if there is an ivar with same name, similar name, or
if the whole thing is synthesized. For example, you could have:
@class Name : NSObject {
NSString *myFirstName;
NSString *myLastName;
}
- (NSString *) firstName;
- (void) setFirstName: (NSString *) name;
- (NSString *) lastName;
- (void) setLastName: (NSString *) name;
- (NSString *) fullName;
@end
where "fullName" is just:
- (NSString *) fullName
{
return [myFirstName stringByAppendingFormat: @" %@", myLastName];
}
Add in
+ (void) initialize
{
[self setKeys: [NSArray arrayWithObjects: @"firstName",
@"lastName" NULL] triggerChangeNotificationsForDependentKey:
@"fullName"];
}
and you can now observe firstName, lastName (both getting and
setting), and fullName, none of which are the names of ivars and one
of which doesn't even exist as an ivar (and has no "setFullName:), yet
everything will work as expected.
Glenn Andreas <email_removed>
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next
generation of fractal art
DATE : Fri Nov 23 19:29:44 2007
On Nov 23, 2007, at 12:09 PM, Philip Mötteli wrote:
>> KVO is not based on the internals of an object (which is assumed to
>> be hidden), and it does not access hiddden internals. KVO is based
>> on KVC and uses accessors.
>
> In order to use KVO, you need to know this famous "key". The key is
> the name of an instance variable, which is an object's internal
> data. Every OOP book will tell you that (<Object-Oriented
> Programming with Objective-C- Classes.webloc>):
No you don't. All that matters is that there is a method "foo" that
returns something, and a corresponding "setFoo:" that takes a similar
type as "foo" returns and you can observe it (and the second method
isn't needed, so long as some mechanism exists to let KVO know when
the value of "foo" has changed)
It doesn't matter if there is an ivar with same name, similar name, or
if the whole thing is synthesized. For example, you could have:
@class Name : NSObject {
NSString *myFirstName;
NSString *myLastName;
}
- (NSString *) firstName;
- (void) setFirstName: (NSString *) name;
- (NSString *) lastName;
- (void) setLastName: (NSString *) name;
- (NSString *) fullName;
@end
where "fullName" is just:
- (NSString *) fullName
{
return [myFirstName stringByAppendingFormat: @" %@", myLastName];
}
Add in
+ (void) initialize
{
[self setKeys: [NSArray arrayWithObjects: @"firstName",
@"lastName" NULL] triggerChangeNotificationsForDependentKey:
@"fullName"];
}
and you can now observe firstName, lastName (both getting and
setting), and fullName, none of which are the names of ivars and one
of which doesn't even exist as an ivar (and has no "setFullName:), yet
everything will work as expected.
Glenn Andreas <email_removed>
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next
generation of fractal art






Cocoa mail archive

