Can I customize @synthesized properties?

I'm probably just a little lazy here, but carrying with me. Here is my situation. I have a class with two non-atomic, stored properties. Let them talk:

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

I synthesize them as expected in the implementation. I want to say that whenever the installer is called on a date, it also does something with the dateField parameter (i.e. sets the text property in DateField as a well-formatted version of the date).

I understand that I can simply manually override setter for a 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 } } 

It would be great if I could let Objective-C handle the save / release cycle, but still be able to "register" (due to the lack of a better term) a user-defined handler that will be called after the save / release / kit happens. I guess this is not possible. My google-fu did not come up with any answer to this question, so I thought I would ask.

+4
source share
2 answers

KVO (key / value monitoring) can do this, sort of, but it will be even more code and probably won't be easier to read or write.

You may be familiar with KVO, but in case you (or others) are not: in your init function, you will do the following:

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

Then you would do 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 will do the following:

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

As you can see, this is even more code and harder to understand. Not very effective for someone whose goal should be lazy :-) But KVO is the main function of Objective-C data binding. There are some platforms (such as Flex) that can bind data with significantly less code, but a lot of work is required in Objective-C.

By the way, it doesn’t matter, but the example code that you showed is buggy - probably it should look something like this:

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

you can execute by creating a private ivar / property (well, you should document the property is private, ideally using an obvious name) with a different name:

 @property (nonatomic, retain) UITextField *dateField; @property (nonatomic, retain) NSDate *date; // interface only @property (nonatomic, retain) NSDate *datePRIVATE; // the real ivar. ... @synthesize datePRIVATE; - (void)setDate:(NSDate *)newDate { self.datePRIVATE = newDate; [self.dateField updateDisplayedDate:self.datePRIVATE]; } 
0
source

Source: https://habr.com/ru/post/1335360/


All Articles