Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7895707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:42:30+00:00 2026-06-03T07:42:30+00:00

Assume that I have a class with a readonly property on it. //MyClass.h @interface

  • 0

Assume that I have a class with a readonly property on it.

//MyClass.h
@interface MyClass

@property (readonly) NSInteger MonitorMe;

@end

Now, let’s assume the point of this property is to monitor changes of another property, within another object, and when the property is “observed” it returns a derived value by inspecting a value from the other, external object.

//MyClass.m
@implementation

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

... Inits and Methods ...
@end

Now, let’s assume that some where I create an instance of the MyClass object, and I want to add a KVO observer on the MonitorMe property.

//AnotherClass.m
@implementation AnotherClass.m

    @synthesize instanceOfMyClass;

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

My question is, since the MonitorMe property only monitors the changes of values in an external object, will the observer method execute when the value of globalStaticClass.OtherNSInteger changes? Also, if the answer is yes, how is this done?

If this works, it would seem like compiler voodoo to me.

Note

I don’t think it makes a difference, but I am using ARC for this implementation and I’m compiling for an iOS device. I doubt there are compilation differences between OS X and iOS for this type of question but, if it matters, I have an iOS project that requires such an implementation outlined above.

Also, the example outlined above is a very basic setup of my actual needs. It could be argued that I could/should add an observation to the globalStaticClass.OtherNSInteger value instead of the readonly property, MonitorMe. In my actual circumstance that answer is not sufficient because my readonly property is much more complex than my example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:42:32+00:00Added an answer on June 3, 2026 at 7:42 am

    will the observer method execute when the value of globalStaticClass.OtherNSInteger changes?

    No, but you can make that happen, via +keyPathsForValuesAffectingMonitorMe (or the more generic +keyPathsForValuesAffectingValueForKey:, if the “globalStaticClass” is actually a property of MyClass. See “Registering Dependent Keys” in the KVO Guide.

    Here’s a quick mockup:

    #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 style notes: properties/variables should have lowercase initial letters, and (this is actually more important now because of ARC) don’t name accessor methods to start with “get” — that has a specific meaning in Cocoa involving passing in a buffer and getting data back by reference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's assume I have a class named ABC that will have a list
Assume I have a class that looks like this: class Sample { public string
Assume that I have this piece of code: @interface Foo : NSObject { Bar
So I have a class that looks like this: internal class MyClass { public
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
Let's assume that I have following class template: <> - read as template template<template<class
It is design problem. Let's assume that we have this kind of model in
Assume that I have three class; UserAccount, UserGroup and Role. UserGroup and Role are
Assume that we have the following code: class Program { static volatile bool flag1;
Assume some domain and view objects (that have no common base class) public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.