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 6038309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:12:03+00:00 2026-05-23T06:12:03+00:00

In order to get started with bindings, I’m writing a small application that converts

  • 0

In order to get started with bindings, I’m writing a small application that converts temperature in Celsius to temperature in Kelvin and vice-versa.

I have two NSTextFields which are binded to two properties known as float kelvin and float centigrade. And, of course, I have two custom setters which set the value for each property appropriately.

The setter for celsius is:

centigrade = value;
[self willChangeValueForKey:@"kelvin"];
kelvin = value + 273;
[self didChangeValueForKey:@"kelvin"];

I need to use willChangeValueForKey: otherwise the value is not updated in the other NSTextField.

My question is, is there a more elegant way to do this? If I add another property and textfield to convert the temperature to Fahrenheit, I will need to add willChange... and didChange... in that getter as well.

Is there a way to tell nib file that these two properties are linked and whenever one changes, it must notify both the observers?

EDIT:

For what its worth, I have tried calling the kelvin’s setter from inside centrigrade’s setter. But if I do that in the setter for kelvins as well, wouldn’t it cause problems? For instance:

centigrade = value;
[self setKelvin:value];

and

kelvin = value;
[self setCelsius:value];
  • 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-23T06:12:04+00:00Added an answer on May 23, 2026 at 6:12 am

    Don’t use willChangeValueForKey: / didChangeValueForKey: here, the notifications are automatic. If you are not seeing the notifications it means you are not using the properties in a key-value coding compliant manner. You don’t need custom setters at all.

    You should only call willChangeValueForKey: / didChangeValueForKey: if you have explicitly disabled automatic kvo for those properties (there are sometimes reasons to do this, but save that for later down the line).

    Once you have that working you can tackle your other problem. Yes, it is possible to have one property change trigger notifications for both properties, but as you have seen that is no use if the second property change also triggers the first again, and so on.

    The problem is that your Model isn’t sophisticated enough to deal with your problem. Forget bindings for a while, forget the presentation altogether until you have the model working. In pseudo-code, ignoring floating point details, you need to at least get something like this working:-

    model.celcius = 100.0;
    assert( model.kelvin == 373.0 )
    
    model.kelvin = 0.0;
    assert( model.celcius == -273.0 )
    

    Assuming you follow the guides on kvo complience (and understand when to use it and when not to – ie, you are in control of when to trigger the notification), you will then be able to hook up your textfield bindings and everything will just work.

    This might look something like:-

    @property (assign) float *temperatureKelvin;    // only one instance variable needed
    
    - (float)temperatureFahrenheit {
      return tempeatureKelvin * ...;                // whatever formula is
    }
    
    - (float)temperatureCelcius {
      return tempeatureKelvin * ...;                // whatever formula is
    }
    
    - (void)setTemperatureFahrenheit:(float)val {
      self.temperatureKelvin = val * ...;           // whatever formula is
    }
    
    - (void)setTemperatureCelcius:(float)val { {
      self.temperatureKelvin = val * ...;           // whatever formula is
    }
    

    The key to making this work is to make sure that updating the value of temperatureKelvin sends a notification that temperatureFahrenheit and temperatureCelcius have changed. You do this by registering them as dependant preoperties..

    + (NSSet *)keyPathsForValuesAffectingTemperatureFahrenheit {
        return [NSSet setWithObject:@"tempeatureKelvin"];
    }
    
    + (NSSet *)keyPathsForValuesAffectingTemperatureCelcius {
        return [NSSet setWithObject:@"tempeatureKelvin"];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to get started developing native (C/C++) OpenGL ES 2.0 applications for
I have a large data set that I want to clean up for the
I'm running a local copy of the railscasts website to get familiar with rails
I am trying to create a Blend behavior related to ComboBoxes. In order to
Hello i am new to Kernel Programming and i would you to answer me
I was thinking of making a new, light-weight database population framework. I absolutely hate
Im trying to perform DFS on a Minimum Spanning Tree which contains 26 nodes.
I managed to install a Python script as a service using this recipe at
I'm starting to play around with Boomerang for measuring performance. It's very promising. While
I am starting over and relearning Drupal, hopefully unlearning some bad habits. I'm reading

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.