• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
problem with pure automatic KVO for array (no controllers...)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

problem with pure automatic KVO for array (no controllers...)


  • Subject: problem with pure automatic KVO for array (no controllers...)
  • From: Michael Monscheuer <email@hidden>
  • Date: Sat, 21 May 2005 23:29:33 +0200

I just had a hard time with KVO.

For to-one relationships my observer gets a NSKeyValueChangeSetting message.
For to-many it does not.
The accessor of my observed instance returns a mutable array.
Observer registers for the key of the mutable array returning accessor of observed instance.
So the observed property is a to-many relationship.
I expected the observer to get a NSKeyValueChangeInsertion info.
It just does not work.
Any idea? You'll find an example project below...

michael

--
=======================================================
//
//  main.m
//  KVO test
//

#import <Cocoa/Cocoa.h>

// -------- observing class -------

@interface DSDFooObserver : NSObject
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
@end


@implementation DSDFooObserver

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
NSLog(@"observer change kind:%@",[change objectForKey:NSKeyValueChangeKindKey]);
}


@end

// ------- observed class ------------

@interface DSDFooTest:NSObject {
    NSMutableArray *testArray;
}
    // simple accessors
-(NSMutableArray *)testArray;
-(void)setTestArray:(NSMutableArray *)newArray;

@end

@implementation DSDFooTest

    // init
-(id)init
{
    self = [super init];
    if (self) {
        testArray = [[NSMutableArray array] retain];
    }

    return self;
}

    // deallocation
-(void) dealloc {
    [testArray release];
    [super dealloc];
}

    // get accesor
-(NSMutableArray *)testArray {
    return testArray;
}

    // set accessor
-(void)setTestArray:(NSMutableArray *)newArray {
    [testArray release];
    testArray = [newArray retain];
}

@end

// ------- main ------------

int main(int argc, char *argv[]) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        // creating observed instance
    DSDFooTest *testObject = [[DSDFooTest alloc] init];
        // creating observing instance
    DSDFooObserver *observer = [[DSDFooObserver alloc] init];

    [testObject addObserver:observer forKeyPath:@"testArray" options:0 context:NULL];

        // this insertion does not inform observer
    [[testObject testArray] insertObject:[[NSObject alloc] init] atIndex:0];

        // setting a new array to observed instance informs observer
    [testObject setTestArray:[NSArray array]];

    [pool release];

    return 0;
}
//
//  main.m
//  KVO test
//

#import <Cocoa/Cocoa.h>

// -------- observing class -------

@interface DSDFooObserver : NSObject
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
@end

@implementation DSDFooObserver

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    NSLog(@"observer change kind:%@",[change objectForKey:NSKeyValueChangeKindKey]);
}

@end

// ------- observed class ------------

@interface DSDFooTest:NSObject {
    NSMutableArray *testArray;
}

    // simple accessors
-(NSMutableArray *)testArray;
-(void)setTestArray:(NSMutableArray *)newArray;

@end

@implementation DSDFooTest

    // init
-(id)init
{
    self = [super init];
    if (self) {
        testArray = [[NSMutableArray array] retain];
    }

    return self;
}

    // deallocation
-(void) dealloc {
    [testArray release];
    [super dealloc];
}

    // get accesor
-(NSMutableArray *)testArray {
    return testArray;
}

    // set accessor
-(void)setTestArray:(NSMutableArray *)newArray {
    [testArray release];
    testArray = [newArray retain];
}

@end

// ------- main ------------

int main(int argc, char *argv[]) {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        // creating observed instance
    DSDFooTest *testObject = [[DSDFooTest alloc] init];
        // creating observing instance
    DSDFooObserver *observer = [[DSDFooObserver alloc] init];

    [testObject addObserver:observer forKeyPath:@"testArray" options:0 context:NULL];

        // this insertion does not inform observer
    [[testObject testArray] insertObject:[[NSObject alloc] init] atIndex:0];

        // setting a new array to observed instance informs observer
    [testObject setTestArray:[NSArray array]];

    [pool release];

    return 0;
}
 _______________________________________________
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

  • Follow-Ups:
    • Re: problem with pure automatic KVO for array (no controllers...)
      • From: mmalcolm crawford <email@hidden>
  • Prev by Date: Re: Strategy for using common classes in multiple Cocoa plug-ins?
  • Next by Date: Re: Photoshop PSD Files and NSView
  • Previous by thread: Re: removing all user defaults from prefs file and 'memory'
  • Next by thread: Re: problem with pure automatic KVO for array (no controllers...)
  • Index(es):
    • Date
    • Thread