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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:58:53+00:00 2026-05-19T09:58:53+00:00

I’m probably just being a bit lazy here, but bear with me. Here’s my

  • 0

I’m probably just being a bit lazy here, but bear with me. Here’s my situation. I have a class with two nonatomic, retained properties. Let’s say:

@property (nonatomic, retain) UITextField *dateField;
@property (nonatomic, retain) NSDate *date;

I synthesize them as expected in the implementation. What I want to happen is that whenever the setter on date is invoked, it also does something to the dateField (i.e. it sets the text property on the dateField to be a nicely formatted version of the date).

I realize I can just manually override the setter for date in my implementation by doing the following:

- (void) setDate:(NSDate *)newDate {
    if (date != newDate) {
        [date release];
        date = [newDate retain];
        // my code to touch the dateField goes here
    }
}

What would be awesome is if I could let Objective C handle the retain/release cycle, but still be able to “register” (for lack of a better term) a custom handler that would be invoked after the retain/release/set happens. My guess is that isn’t possible. My google-fu didn’t come up with any answer to this, though, so I thought I’d ask.

  • 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-19T09:58:54+00:00Added an answer on May 19, 2026 at 9:58 am

    KVO (key/value observing) can do this, sort of, but it would end up being even more code, and probably no easier to read or to write.

    You may be familiar with KVO, but in case you (or others) aren’t: In your init function, you would do this:

    [self addObserver:self forKeyPath:@"date" options:0 context:NULL];
    

    Then you would implement this:

    -(void)observeValueForKeyPath:(NSString*)keyPath 
                         ofObject:(id)object
                           change:(NSDictionary*)change
                          context:(void*)context
    {
        if (object == self && [keyPath isEqualToString:@"date"]) {
            // code to touch the dateField goes here
        }
    }
    

    Finally, in dealloc, you would do this:

    [self removeObserver:self forKeyPath:@"date"];
    

    As you can see, that’s even more code, and harder to understand. Not really very effective for someone whose goal is to be lazy 🙂 But KVO is the main “data binding” functionality of Objective-C. There are some platforms (e.g. Flex) which can do data binding with a whole lot less code, but it takes a lot of work in Objective-C.

    By the way, not a big deal, but the sample code you showed is buggy — it should probably look more like this:

    - (void) setDate:(NSDate *)newDate {
        if (date != newDate) {
            [date release];
            date = [newDate retain];
            // my code to touch the dateField goes here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.