• 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
tearing my hair, ok here is a compilable example.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

tearing my hair, ok here is a compilable example.


  • Subject: tearing my hair, ok here is a compilable example.
  • From: Chris Idou <email@hidden>
  • Date: Wed, 15 Oct 2008 22:13:16 -0700 (PDT)

I've attached code that shows what I'm talking about with my keyPathsForValuesAffectingValueForKey problem. If anyone wants me to send a zip file (60kb) xcode project, jet let me know. For those who just want to see the code, I've appended it here also. To compile, just put the 2 classes in a project. Make a NSObjectController and NSObject in IB, link class2 to class1 outlet, and create a button linked to change: to make it do stuff.

Can anyone tell me why myMethod isn't called by observeValueForKeyPath when you press the button and call change: ?

#import <Cocoa/Cocoa.h>


@interface Class1 : NSObjectController {
    BOOL canLink;
}
-(BOOL)canLink;

- (IBAction)change:(id)v;


@end


#import "Class1.h"


@implementation Class1

-(BOOL)canLink {
    NSLog(@"canLink: %d nc:%d", canLink, self);
    return canLink;
}


- (IBAction)change:(id)v; {
    [self willChangeValueForKey:@"canLink"];
    canLink = !canLink;
    [self didChangeValueForKey:@"canLink"];
    NSLog(@"change:%d", canLink);
}

@end

#import <Cocoa/Cocoa.h>

@class Class1;
@interface Class2 : NSObject {
    IBOutlet Class1 *class1;


}
@property(readonly) Class1 *class1;
-(BOOL)myMethod;
@end

#import "Class2.h"
#import "Class1.h"

@implementation Class2

@synthesize class1;

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {
    NSLog(@"ovfkp: %@", keyPath);
    if (context == [Class2 class]) {
        if ([keyPath isEqualToString:@"myMethod"]) {
            NSLog(@"newvalue - myMethod:%d", [self myMethod]);
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)awakeFromNib {
    [self addObserver:self
           forKeyPath:@"myMethod"
              options:NSKeyValueObservingOptionNew
              context: [Class2 class]];

}


+ (NSSet *)keyPathsForValuesAffectingMyMethod {
    NSLog(@"keyPathsForValuesAffectingMyMethod");
    return [NSSet setWithObject:@"class1.canLink"];
}

-(BOOL)myMethod {
    return [class1 canLink];
}


@end





_______________________________________________

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: tearing my hair, ok here is a compilable example.
      • From: Quincey Morris <email@hidden>
  • Prev by Date: CoreData: "Unacceptable type of value for to-one relationship"
  • Next by Date: Re: tearing my hair, ok here is a compilable example.
  • Previous by thread: CoreData: "Unacceptable type of value for to-one relationship"
  • Next by thread: Re: tearing my hair, ok here is a compilable example.
  • Index(es):
    • Date
    • Thread