If I write a getter method for a custom property, will KVO work if getter returns a value by referring to the value of another object?

Suppose I have a class with readonly property.

//MyClass.h
@interface MyClass

@property (readonly) NSInteger MonitorMe;

@end

Now suppose the point of this property is to track changes in another property inside another object, and when the property is "observed", it returns the derived value by checking the value from another external object.

//MyClass.m
@implementation

@synthesize MonitorMe;
-(NSInteger) getMonitorMe
{
    return globalStaticClass.OtherNSInteger;
}

... Inits and Methods ...
@end

Now suppose some where I instantiate an object MyClassand I want to add a KVO observer to the property MonitorMe.

//AnotherClass.m
@implementation AnotherClass.m

    @synthesize instanceOfMyClass;

    -(id)init
    {
        ...
        instanceOfMyMethod = [MyClass init];
            [MyClass addObserver: self 
                  forKeyPath: @"MonitorMe" 
                     options: NSKeyValuObservingOptionNew
                     context: nil];
        ...
    }

, , MonitorMe , globalStaticClass.OtherNSInteger? , , , ?

, voodoo.

, , ARC , iOS. , OS X iOS , , iOS, , .

, . , / globalStaticClass.OtherNSInteger readonly, MonitorMe. , , readonly .

+5
1

globalStaticClass.OtherNSInteger?

, +keyPathsForValuesAffectingMonitorMe ( +keyPathsForValuesAffectingValueForKey:, "globalStaticClass" MyClass. . " " KVO.

:

#import <Foundation/Foundation.h>

@interface Monitored : NSObject 
@property NSInteger otherInteger;
@end

@implementation Monitored
@synthesize otherInteger;
@end

@interface Container : NSObject 
@property (readonly) NSInteger monitorMe;
@property (strong) Monitored * theMonitored;

- (void)changeMonitoredInteger;
@end

@implementation Container

@synthesize theMonitored;

+ (NSSet *)keyPathsForValuesAffectingMonitorMe {

    return [NSSet setWithObject:@"theMonitored.otherInteger"];
}

- (id) init {

    self = [super init];
    if( !self ) return nil;

    theMonitored = [[Monitored alloc] init];
    [theMonitored setOtherInteger:25];

    return self;
}

- (NSInteger)monitorMe
{
    return [[self theMonitored] otherInteger];
}

- (void)changeMonitoredInteger {

    [[self theMonitored] setOtherInteger:arc4random()];
}

@end

@interface Observer : NSObject 
@end

@implementation Observer

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

    NSLog(@"Observing change in: %@ %@", keyPath, object);
}

@end

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

    @autoreleasepool {

        Observer * o = [[Observer alloc] init];
        Container * c = [[Container alloc] init];

        [c addObserver:o
            forKeyPath:@"monitorMe"
               options:NSKeyValueObservingOptionNew
               context:NULL];

        [c changeMonitoredInteger];
        [c changeMonitoredInteger];

    }
    return 0;
}

P.S. Cocoa : / ( - ARC) "get" - Cocoa .

+8

All Articles