• 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
Screwy Binding/KVO
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Screwy Binding/KVO


  • Subject: Screwy Binding/KVO
  • From: Steven Degutis <email@hidden>
  • Date: Fri, 11 Dec 2009 11:10:31 -0600

Hi,

Sorry if this has been posted before; I did a quick scour but might not have
used the right keywords (this issue isn't very general).

Basically I'm noticing that when I expect KVO observations to be triggered,
they aren't. Most likely this is due to my misunderstanding, but I'd like to
post a test case here for good measure (I write this last night so bear with
me if the comments are a little strange).



#import <Foundation/Foundation.h>

#import <AppKit/AppKit.h>


@interface SDObject : NSObject {

NSArray *content;

}

@property (readwrite, retain) NSArray *content;

@end



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

[NSAutoreleasePool new];

 SDObject *object = [[SDObject alloc] init];

 NSArrayController *controller1 = [[NSArrayController alloc] init];

NSArrayController *controller2 = [[NSArrayController alloc] init];

 NSLog(@"mark 1");

 // when this is called, the KVO in SDObject is triggered with an empty
array

[object bind:@"content"

toObject:controller2

 withKeyPath:@"arrangedObjects"

 options:nil];

 NSLog(@"mark 2");

 [controller2 bind:@"contentArray"

 toObject:controller1

  withKeyPath:@"arrangedObjects"

  options:nil];

 NSLog(@"mark 3");

 // these might suppose to trigger it (i thought it would but it doesnt)

[controller1 addObject:@"test1"];

[controller1 addObject:@"test2"];

 // okay i *know* this and the -didChange... should trigger it

[controller2 willChangeValueForKey:@"arrangedObjects"];

 // this should too though, right?

[controller2 rearrangeObjects];

 // (see -willChange... above)

[controller2 didChangeValueForKey:@"arrangedObjects"];

 // proving that they all contain the right things

NSLog(@"%@", [controller2 arrangedObjects]);

NSLog(@"%@", [object content]);

 return EXIT_SUCCESS;

}



@implementation SDObject


@synthesize content;


+ (void) initialize {

if (self == [SDObject class]) {

[self exposeBinding:@"content"];

}

}


- (id) init {

self = [super init];

 [self addObserver:self

   forKeyPath:@"content"

  options:0

  context:NULL];

 return self;

}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {

NSLog(@"observing keypath [%@] == [%@]", keyPath, [self valueForKeyPath
:keyPath]);

}


@end



This is the output:


*2009-12-11 11:10:02.139 ScrewyKVO[21311:a0f] mark 1*

*2009-12-11 11:10:02.515 ScrewyKVO[21311:a0f] observing keypath [content] ==
[(*

*)]*

*2009-12-11 11:10:02.516 ScrewyKVO[21311:a0f] mark 2*

*2009-12-11 11:10:02.521 ScrewyKVO[21311:a0f] mark 3*

*2009-12-11 11:10:02.522 ScrewyKVO[21311:a0f] (*

*    test1,*

*    test2*

*)*

*2009-12-11 11:10:02.522 ScrewyKVO[21311:a0f] (*

*    test1,*

*    test2*

*)*
*
*


--
Steven Degutis
http://www.thoughtfultree.com/
http://www.degutis.org/
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Screwy Binding/KVO
      • From: Quincey Morris <email@hidden>
  • Prev by Date: Re: Outing the poseAsClass: poseur
  • Next by Date: NSProgress Indicator
  • Previous by thread: Re: NSSavePanel crash!
  • Next by thread: Re: Screwy Binding/KVO
  • Index(es):
    • Date
    • Thread