Re: KVO of nested objects
Re: KVO of nested objects
- Subject: Re: KVO of nested objects
- From: Bruce Truax <email@hidden>
- Date: Tue, 26 Oct 2004 08:54:47 -0400
Here is the important snippets of source code. The first section is the
nested object followed by the master object. The final section is the
controller which also creates the array of master objects. The source code
for the controller includes the add and remove observer functions. Note
that most of the observed keys are passed made available in pre-defined
arrays of keys supplied by the objects. The one key that I am adding for
the nested object (isOn) is just added manually for now.
Thanks.
Bruce
NESTED OBJECT
@interface ACPikupData : NSObject {
BOOL isOn;
int surface;
float scale;
float offset;
}
- (int)surface;
- (void)setSurface:(int)newSurface;
- (float)scale;
- (void)setScale:(float)newScale;
- (float)offset;
- (void)setOffset:(float)newOffset;
- (BOOL)isOn;
- (void)setIsOn:(BOOL)newIsOn;
@end
#import "ACPikupData.h"
@implementation ACPikupData
- (int)surface {
return surface;
}
- (void)setSurface:(int)newSurface {
if (surface != newSurface) {
surface = newSurface;
}
}
- (float)scale {
return scale;
}
- (void)setScale:(float)newScale {
if (scale != newScale) {
scale = newScale;
}
}
- (float)offset {
return offset;
}
- (void)setOffset:(float)newOffset {
if (offset != newOffset) {
offset = newOffset;
}
}
- (BOOL)isOn {
return isOn;
}
- (void)setIsOn:(BOOL)newIsOn {
if (isOn != newIsOn) {
isOn = newIsOn;
}
}
@end
MASTER OBJECT (partial)
@interface ACSurface : NSObject {
NSArray *lensParameterList;
int surfaceNumber;
BOOL astop;
float rd;
float rdx;
float rdy;
float th;
NSColor *thColor;
NSColor *rdColor;
NSString *label;
NSString *glass;
NSString *glassType;
BOOL asph;
float cc;
float ad;
float ae;
float af;
float ag;
float yd;
float xd;
float alpha;
float beta;
float gamma;
ACPikupData *thPikup;
ACPikupData *rdPikup;
ACPikupData *rdrPikup;
ACPikupData *glassPikup;
ACPikupData *ccPikup;
ACPikupData *adPikup;
ACPikupData *aePikup;
ACPikupData *afPikup;
ACPikupData *agPikup;
ACPikupData *alphaPikup;
ACPikupData *betaPikup;
ACPikupData *gammaPikup;
ACPikupData *xdPikup;
ACPikupData *ydPikup;
ACPikupData *proPikup;
ACPikupData *nproPikup;
}
@implementation ACSurface
- (id)init
{
self = [super init];
if (self) {
surfaceNumber = 0;
[self setAstop:NO];
rd =0.0;
rdx = 0.0;
rdy = 0.0;
th = 0.0;
thPikup = [[ACPikupData alloc] init];
rdPikup = [[ACPikupData alloc] init];
rdrPikup = [[ACPikupData alloc] init];
glassPikup = [[ACPikupData alloc] init];
ccPikup = [[ACPikupData alloc] init];
adPikup = [[ACPikupData alloc] init];
aePikup = [[ACPikupData alloc] init];
afPikup = [[ACPikupData alloc] init];
agPikup = [[ACPikupData alloc] init];
alphaPikup = [[ACPikupData alloc] init];
betaPikup = [[ACPikupData alloc] init];
gammaPikup = [[ACPikupData alloc] init];
xdPikup = [[ACPikupData alloc] init];
ydPikup = [[ACPikupData alloc] init];
proPikup = [[ACPikupData alloc] init];
nproPikup = [[ACPikupData alloc] init];
}
- (ACPikupData *)rdPikup {
return [[rdPikup retain] autorelease];
}
- (void)setRdPikup:(ACPikupData *)newRdPikup {
if (rdPikup != newRdPikup) {
[rdPikup release];
rdPikup = [newRdPikup copy];
}
}
- (ACPikupData *)rdrPikup {
return [[rdrPikup retain] autorelease];
}
- (void)setRdrPikup:(ACPikupData *)newRdrPikup {
if (rdrPikup != newRdrPikup) {
[rdrPikup release];
rdrPikup = [newRdrPikup copy];
}
}
- (ACPikupData *)glassPikup {
return [[glassPikup retain] autorelease];
}
- (void)setGlassPikup:(ACPikupData *)newGlassPikup {
if (glassPikup != newGlassPikup) {
[glassPikup release];
glassPikup = [newGlassPikup copy];
}
}
.
.
.
@end
CONTROLLER
@interface ACLensDataObject : NSObject
{
IBOutlet NSMutableArray *surfaceArray; //Array of ACSurface Objects
IBOutlet NSTableView *theTableView;
IBOutlet NSArrayController *theArrayController;
NSString *lensIdentifier;
NSString *lic1;
NSString *lic2;
NSString *lic3;
NSString *lic4;
NSString *units;
}
- (void)addObserversForAllSurfaceKeysAtSurface:(int)observerSurfaceNumber;
-
(void)removeObserversForAllSurfaceKeysAtSurface:(int)observerSurfaceNumber;
- (void)addObserversForAllSystemKeys;
- (void)removeObserversForAllSystemKeys;
@implementation ACLensDataObject
- (void)addObserversForAllSurfaceKeysAtSurface:(int)observerSurfaceNumber
{
NSArray *surfaceKeys;
NSEnumerator *enumerator;
NSString *aKey;
NSRange surfaceArrayRange;
NSIndexSet *surfaceArrayIndexSet;
int index;
//if the surfaceNumber is less than 0 then we are doing the entire
//surfaceArray. Otherwise we are just doing the surface passed into the
routine
if (!isObservingSurfaceParameters){
if (observerSurfaceNumber < 0){
index = 0;
surfaceArrayRange.location = 0;
surfaceArrayRange.length = [surfaceArray count];
}else{
index = observerSurfaceNumber;
surfaceArrayRange.location = observerSurfaceNumber;
surfaceArrayRange.length = 1;
}
ACSurface *theSurface = [surfaceArray objectAtIndex:index];
surfaceKeys = [theSurface lensParameterList];
enumerator = [surfaceKeys objectEnumerator];
surfaceArrayIndexSet = [NSIndexSet
indexSetWithIndexesInRange:surfaceArrayRange];
// [surfaceArrayIndexSet autorelease];
while ( aKey = [enumerator nextObject]){
[surfaceArray addObserver:self
toObjectsAtIndexes: surfaceArrayIndexSet
forKeyPath:aKey
options:NSKeyValueObservingOptionNew
context:aSurface];
}
//Here is where I attempt to turn on observing for the nested objects
[surfaceArray addObserver:self
toObjectsAtIndexes: surfaceArrayIndexSet
forKeyPath:@"isOn"
options:NSKeyValueObservingOptionNew
context:aSurface];
isObservingSurfaceParameters = YES;
}
}
- (void)removeObserversForAllSurfaceKeysAtSurface:(int)observerSurfaceNumber
{
NSArray *surfaceKeys;
NSEnumerator *enumerator;
NSString *aKey;
NSRange surfaceArrayRange;
NSIndexSet *surfaceArrayIndexSet;
int index;
if (isObservingSurfaceParameters){
//if the surfaceNumber is less than 0 then we are doing the entire
//surfaceArray. Otherwise we are just doing the surface passed into
the routine
if (observerSurfaceNumber < 0){
index = 0;
surfaceArrayRange.location = 0;
surfaceArrayRange.length = [surfaceArray count];
}else{
index = observerSurfaceNumber;
surfaceArrayRange.location = observerSurfaceNumber;
surfaceArrayRange.length = 1;
}
ACSurface *theSurface = [surfaceArray objectAtIndex:index];
surfaceKeys = [theSurface lensParameterList];
enumerator = [surfaceKeys objectEnumerator];
surfaceArrayIndexSet = [NSIndexSet
indexSetWithIndexesInRange:surfaceArrayRange];
// [surfaceArrayIndexSet autorelease];
while ( aKey = [enumerator nextObject]){
[surfaceArray removeObserver:self
fromObjectsAtIndexes: surfaceArrayIndexSet
forKeyPath:aKey];
}
isObservingSurfaceParameters = NO;
}
}
- (void)addObserversForAllSystemKeys
{
NSArray *systemKeys;
NSEnumerator *enumerator;
NSString *aKey;
if (!isObservingSystemParameters){
systemKeys = [self systemParameterList];
enumerator = [systemKeys objectEnumerator];
while ( aKey = [enumerator nextObject]){
[self addObserver:self
forKeyPath:aKey
options:NSKeyValueObservingOptionNew
context:aSurface];
}
isObservingSystemParameters = YES;
}
}
- (void)removeObserversForAllSystemKeys
{
NSArray *systemKeys;
NSEnumerator *enumerator;
NSString *aKey;
if(isObservingSystemParameters){
systemKeys = [self systemParameterList];
enumerator = [systemKeys objectEnumerator];
while ( aKey = [enumerator nextObject]){
[self removeObserver:self
forKeyPath:aKey];
}
isObservingSystemParameters = NO;
}
}
_______________________________________________
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