#import
@interface Person : NSObject {
NSString *firstName;
NSString *lastName;
}
- (void)setFirstName:(NSString *)value;
- (NSString *)firstName;
- (void)setLastName:(NSString *)value;
- (NSString *)lastName;
- (NSString *)fullName;
@end
#import "Person.h"
@implementation Person
- (void)dealloc{
[firstName release], firstName = nil;
[lastName release], lastName = nil;
[super dealloc];
}
- (void)setFirstName:(NSString *)value{
if ([firstName isEqualToString:value]) return;
[self willChangeValueForKey:@"fullName"];
[firstName release];
firstName = [value retain];
[self didChangeValueForKey:@"fullName"];
}
- (NSString *)firstName{
return [[firstName retain] autorelease];
}
- (void)setLastName:(NSString *)value{
if ([lastName isEqualToString:value]) return;
[self willChangeValueForKey:@"fullName"];
[lastName release];
lastName = [value retain];
[self didChangeValueForKey:@"fullName"];
}
- (NSString *)lastName{
return [[lastName retain] autorelease];
}
- (NSString *)fullName{
return [NSString stringWithFormat:@"%@ %@",self.firstName,self.lastName];
}
@end
/* Return a set of key paths for properties whose values affect the value of the keyed property. When an observer for the key is registered with an instance of the receiving class, KVO itself automatically observes all of the key paths for the same instance, and sends change notifications for the key to the observer when the value for any of those key paths changes. The default implementation of this method searches the receiving class for a method whose name matches the pattern +keyPathsForValuesAffecting
This method and KVO's automatic use of it comprise a dependency mechanism that you can use instead of sending -willChangeValueForKey:/-didChangeValueForKey: messages for dependent, computed, properties.
You can override this method when the getter method of one of your properties computes a value to return using the values of other properties, including those that are located by key paths. Your override should typically invoke super and return a set that includes any members in the set that result from doing that (so as not to interfere with overrides of this method in superclasses).
You can't really override this method when you add a computed property to an existing class using a category, because you're not supposed to override methods in categories. In that case, implement a matching +keyPathsForValuesAffecting
*/
+ (NSSet *)keyPathsForValuesAffectingFullName{
return [NSSet setWithObjects:@"firstName",@"lastName"];
}
#import "Person.h"
@implementation Person
@synthesize firstName, lastName;
- (void)dealloc{
[firstName release], firstName = nil;
[lastName release], lastName = nil;
[super dealloc];
}
- (NSString *)fullName{
return [NSString stringWithFormat:@"%@ %@",self.firstName,self.lastName];
}
+ (NSSet *)keyPathsForValuesAffectingFullName{
return [NSSet setWithObjects:@"firstName",@"lastName"];
}
@end
#import
@interface Person : NSObject {
NSString *firstName;
NSString *lastName;
}
@property (retain) NSString *firstName;
@property (retain) NSString *lastName;
@property (readonly) NSString *fullName;
@end