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

  • Home
  • SEARCH
  • 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 6723497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:34:33+00:00 2026-05-26T09:34:33+00:00

I want to set up something like this in my environment. [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self

  • 0

I want to set up something like this in my environment.

[[NSUserDefaultsController sharedUserDefaultsController] addObserver:self
    forKeyPath:[@"values." stringByAppendingString: @"MyPreference"]
    options:NSKeyValueObservingOptionNew
    context:NULL];

I’m doing this from a Smalltalk environment. The particular Smalltalk can actually drive the above thru the objective-c runtime features. The problem is the “self” there as the addObserver: argument. From Smalltalk, I can create an C function pointer that will act as a callback into Smalltalk. But it can’t offer it’s notion of what an Object is to the ObjectiveC environment.

So I’m trying to figure out how to trampoline from some sort of object with the appropriate API that causes the function pointer I create to be executed. I’ve looked at NSInvokation and friends, but none of those are used to wrap around a C function pointer. And I’m not sure they’d translate the addObserver:forKeyPath:options:context: to do the right thing.

Boiled down the question is, how do you use existing Cocoa objects to register a response to a Notification that is the execution of a C function pointer without compiling any new objc code.

  • 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-05-26T09:34:34+00:00Added an answer on May 26, 2026 at 9:34 am

    You’ll need to create a glue class in Objective-C. You could do this:

    typedef void TravisGriggsCallback();
    
    @interface TravisGriggsGlue {
        TravisGriggsCallback *_callback;
    }
    - (id)initWithCallback:(TravisGriggsCallback *)callback;
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
        change:(NSDictionary *)change context:(void *)context
    @end
    
    @implementation TravisGriggsGlue
    
     - (id)initWithCallback:(TravisGriggsCallback *)callback
    {
        if (!(self = [super init]))
            return nil;
        _callback = callback;
        return self;
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
        change:(NSDictionary *)change context:(void *)context
    {
        _callback();
    }
    
    @end
    

    If you need to pass arguments to the callback, you’ll need to add instance variables to hold the arguments, and pass them to the init method.

    Then you use it like this:

    TravisGriggsGlue *glue = [[TravisGriggsGlue alloc]
        initWithCallback:&someCallbackFunction];
    [[NSUserDefaultsController sharedUserDefaultsController]
        addObserver:self
        forKeyPath:[@"values." stringByAppendingString: @"MyPreference"]
        options:NSKeyValueObservingOptionNew
        context:NULL];
    [glue release]; // only needed if not using ARC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set something up so that if an Account within my app
I want to set something in the middle of the screen thanks
If i want to set a colour property to something thats non-standard (i.e. not
I want to move something a set distance. However in my system there is
for our project we need to set several PHP values depending on the environment
I want to set up data binding using a custom component. Basically, I'd like
How to read the system environment variable in the application context? I want something
I want set interfaceOrientation in one UIView as LandscapeLeft and Portrait in other UIView.
i want set the visibility to itemized overlay in map view. if the zoom
I want set Listbox background to transparent but not working Is there any idea?

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.