Re: KVO of nested objects
Re: KVO of nested objects
- Subject: Re: KVO of nested objects
- From: Bruce Truax <email@hidden>
- Date: Thu, 28 Oct 2004 21:20:08 -0400
Thanks for the suggestion. So what you are saying is that it is not
possible to observe the items of objects within objects. If I understand
correctly you suggesting that I create a dictionary within my ACSurface
object and that this dictionary contain dictionaries such as rdPikup and
thPikup, each of which contain the isOn, surface, scale and offset keys. I
then tell my program to observe the isOn key and whenever the isOn key in
any one of the xxPikup dictionaries is modified I will be sent a
notification. What I do not understand is how I will tell which of the
[xxPikup isOn] keys was modified. Also, how do I then bind these keys to my
UI elements? Do I create an object controller for each xxPikup key
contained in my ACSurface dictionary?
Bruce
>
Your structure is like this:
>
-----------------
>
ACLensDataObject
>
>
ACSurface x (n)
>
>
ACPikupData x (16 exactly)
>
>
BOOL isOn;
>
-----------------
>
>
>
>
With this code:
>
-----------------
>
@implementation ACLensDataObject
>
>
-
>
(void)addObserversForAllSurfaceKeysAtSurface:(int)observerSurfaceNumber
>
{
>
...
>
[surfaceArray addObserver:self
>
toObjectsAtIndexes: surfaceArrayIndexSet
>
forKeyPath:@"isOn"
>
options:NSKeyValueObservingOptionNew
>
context:aSurface];
>
}
>
-----------------
>
>
>
What this does is attempt to register for notifications on the key
>
"isOn" in the ACSurfaces. Obviously, this won't work. :)
>
>
Part of reason this whole thing seems difficult is that you have a list
>
of 16 individual hardcoded references to ACPikupData objects in each
>
ACSurface object. You should definitely move these into a dictionary
>
with keys like @"thPikup", @"rdPikup", etc. You can make the keys
>
NSString constants.
>
>
With the dictionary in place, the code could look like this:
>
>
-----------------
>
@implementation ACLensDataObject
>
>
-
>
(void)addObserversForAllSurfaceKeysAtSurface:(int)observerSurfaceNumber
>
{
>
...
>
[surfaceArray makeObjectsPerformSelector:
>
@selector(observePikupDataWithObserver:)
>
withObject: self];
>
}
>
>
@implementation ACSurface
>
>
- (void)observePikupDataWithObserver: (id)observer
>
{
>
NSArray *pikupArray = [pikupDictionary allValues];
>
>
[pikupArray addObserver:observer
>
toObjectsAtIndexes: allIndexSet // be sure to calculate this
>
first
>
forKeyPath:@"isOn"
>
options:NSKeyValueObservingOptionNew
>
context:NULL]; // not sure what you want here
>
}
>
-----------------
>
>
Also, you might want to go with something like "active" instead of "on":
>
>
- (BOOL) isActive;
>
- (void) setActive: (BOOL)newActive;
>
>
>
Hope this helps,
>
>
- Scott
>
>
_______________________________________________
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