How to provide additional custom implementation of access methods when using @synthesize?

I want to run some code when the property is available and changed. I use @property and @synthesize in my code for my ivars. The properties are saved, so I would like to keep this memory management material automatically generated by @synthesize .

However, I assume that @synthesize tells the compiler to generate accessor code where @synthesize is , so most of the cases are at the top of the code, right?

And when I have the foo property, I get the -setFoo and -setFoo methods. Can I then make this way to execute another user code when the property changes?

 -(void)setFoo { // custom stuff } 

Now that is the problem. How to complete the first? I would not like to have another name here. Is it possible for the @synthesize directive @synthesize create other names for the getter and setter methods, which I then call easily? And can I still use dot syntax to access them?

+6
objective-c cocoa-touch cocoa accessor
source share
4 answers

You can use @property and @synthesize in the same way as usual, but provide a custom setter or getter (or both), and they will be used instead. Usually I will do something like this:

 // Override the setter - (void)setName:(NSString *)aName { if (name == aName) return; [name release]; name = [aName retain]; //custom code here } 

When I use the set property, it will call my own method. However, the receipt will still be synthesized.

+5
source share

If you provide the ability to create setters or getters, it will use this instead of the generated implementation. It is not difficult to implement the โ€œpreservingโ€ aspect of getters and setters that the compiler generates when you synthesize u, so you can just write your own getters and setters, which I would say and go with that.

+1
source share

One of the awkward decisions is to create an abstract superclass that gives you a normal synthesis of properties. Then create a specific subclass that you are actually using and that simply implements and overrides the method (same signature) and calls super to do the actual setup. This allows you to do what you want to do before or after calling the super implementation.

Example:

 @interface ALTOClassA : NSObject @property NSString *catName; @end 

Nothing more is needed in the .m outside the stub file for this test.

Create a subclass, nothing special is needed in @interface

 #import "ALTOClassA.h" @interface ALTOClassAJunior : ALTOClassA @end 

At @implementation, we do our redefinition.

 #import "ALTOClassAJunior.h" @implementation ALTOClassAJunior - (void)setCatName:(NSString*)aCatName { NSLog(@"%@",NSStringFromSelector(_cmd)); [super setCatName:aCatName]; NSLog(@"after super: self.catName %@", self.catName); } @end 

Using:

  ALTOClassAJunior *aCAJ = [ALTOClassAJunior new]; NSLog(@"aCAS.catName %@", aCAJ.catName); NSLog(@"set it to George."); [aCAJ setCatName:@"George"]; NSLog(@"aCAS.catName %@", aCAJ.catName); 

This allows you to use auto-generated code and still do what you want to do with your class. Abstract A superclass is often a useful solution for many things.

0
source share

Yes, in your @property declaration, you can specify getter and setter methods.

 @property (readwrite,getter=privateGetFoo,setter=privateSetFoo:) NSObject * foo; 

In your methods foo and setFoo: call [self privateGetFoo] or [self privateSetFoo:f] , and then your own code.

An object can also set an observer on its own using addObserver:forKeyPath:options:context:

However, I do not think that any of them is a very clean way to do something. It is better to write your own getter / setter, as others have suggested.

-2
source share

All Articles