Firdous
Firdous

Reputation: 4652

Define setter method on property set

Is there a way to define a setter method that will run on setting a property like:

i want to call

object.something = 0;

meanwhile in object class i want to achieve something like

- (void)setSomething:(NSInteger)something{
    self.something = something;

    // some other work too
}

Upvotes: 0

Views: 630

Answers (2)

Anton Tyutin
Anton Tyutin

Reputation: 644

What you want is called property. you define property in class @interface like:

@interface MyClass()
@property (strong, nonatomic) SomeClass *object;
@end

It will automatically create ivar _object, setter and getter for it. You can override accessor methods. But if you override both setter and getter, you need to synthesize property like:

@implementation MyClass
@synthesize object = _object;
//setter
- (void)setObject:(SomeClass *)object
{
   _object = object;
}
//getter
- (SomeClass *)object
{
   return _object;
}
//class implementation
@end

Upvotes: 3

The dude
The dude

Reputation: 7924

You can do it like this:

@property (nonatomic, weak, setter = setSomething:) UIImageView *photoImageView;

Anyway, setSomething: is the default method for a property named something. You just need to replace self.something with _something, as pointed in the comments.

Upvotes: 0

Related Questions