I need to, for example, execute NSLog(@"Executed.") every time my synthesized getter or setter gets called. I see 2 ways to do that:
- Find some snippets that work probably like synthesized ones. This thread may help in that.
- Use KVO: add some observer which will do the work.
All of them doesn’t looks satisfactory clean for me.
So, subj.
UPDAE: Thank for answers, but directly overriding isn’t a solution:
we loose synthesized code. If we “copy paste” “right” synthesized code from somewhere (even from apple forum where apple engineer gives us code) we should check that it isn’t changed after next compiler release.
You can write an additional property, with your custom getter and setter, that does your own thing, and then accesses the @synthesized ones, like so:
Foo.h:
Foo.m:
and then your code:
So you would use “bar2” as your property, but you get all the niceties out of the @synthesized bar. Anything in bar would be set/get in a thread safe manner, and any additional logic in bar2 would not (which may not matter to you)