Skip navigation.
 
mlRe: With What does Apple Swizzle the IsA-pointer in KVO?
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

Related mailsAuthorDate
mlWith What does Apple Swizzle the IsA-pointer in KVO? Philip Mötteli Nov 23, 14:42
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Jean-Daniel Dupas Nov 23, 14:56
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Clark Cox Nov 23, 15:54
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Philip Mötteli Nov 23, 17:03
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Jean-Daniel Dupas Nov 23, 17:23
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Paul Sargent Nov 23, 17:50
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Clark Cox Nov 23, 18:05
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Philip Mötteli Nov 23, 19:09
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Philip Mötteli Nov 23, 19:09
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Sherm Pendley Nov 23, 19:09
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? mmalc crawford Nov 23, 19:25
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? glenn andreas Nov 23, 19:29
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Philip Mötteli Nov 23, 19:33
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Sherm Pendley Nov 23, 20:01
mlRe: With What does Apple Swizzle the IsA-pointer in KVO? Clark Cox Nov 23, 20:26