Re: With What does Apple Swizzle the IsA-pointer in KVO?
Re: With What does Apple Swizzle the IsA-pointer in KVO?
- Subject: Re: With What does Apple Swizzle the IsA-pointer in KVO?
- From: glenn andreas <email@hidden>
- Date: Fri, 23 Nov 2007 12:29:44 -0600
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@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | prime : build, mutate, evolve, animate : the next
generation of fractal art
_______________________________________________
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