38

There is Class A with:

@interface ClassA : NSObject {
}
@property (nonatomic, assign) id prop1;
@end

@implementation
@synthesize prop1;
@end

then I have subclass

@interface ClassB : ClassA {
}
@end

@implementation

- (id)init {
    self = [super init];
    if (self) {
    }
    return self;
}

//This is infinite loop
- (void) setProp1:(id)aProp
{
    self.prop1 = aProp;
}
@end

and this is infinite loop because setProp1 from ClassB calls [ClassB setProp1:val] from within ClassB.

I've already tried call [super setProp1] but this

How to overwrite @property and assign value inside overwritten setter ? And let's assume I can't modify ClassA.

5
  • “I've already tried call [super setProp1] but this”...? What was the problem? super is correct here. Jun 19, 2011 at 22:16
  • super change value in super only. self.prop1 is null and super.prop1 have value. self->prop1 do the trick (as Sherm suggest)
    – Marcin
    Jun 19, 2011 at 22:25
  • 1
    self.prop1 and super.prop1 should definitely not be returning different values. Are you overriding the getter method too? Jun 19, 2011 at 22:28
  • yes I do and it's return "aProp" from this setter.
    – Marcin
    Jun 19, 2011 at 22:33
  • Shouldn't the superclass's implementation of the getter do that already? Jun 19, 2011 at 22:38

5 Answers 5

58

Just assign to the instance variable directly, without using dot syntax to call the setter:

- (void) setProp1:(id)aProp
{
    self->prop1 = aProp;
}

That kind of begs the question though. All this accessor does is exactly what the parent would have done - so what's the point of overriding the parent at all?

6
  • 3
    example is just for example purpose.
    – Marcin
    Jun 19, 2011 at 22:29
  • 2
    No need for the self-> part unless using GCC 4.2.
    – bbum
    Jun 20, 2011 at 0:09
  • 3
    True, but AFAIK it does no harm, and it serves as a visual reminder that I'm doing something a bit out of the ordinary, accessing an ivar without going through its accessor method. Jun 20, 2011 at 0:20
  • This didn't work out for me, please check out my solution: stackoverflow.com/a/12465444/662605
    – Daniel
    Sep 17, 2012 at 18:54
  • @Daniel You have exactly what he has.
    – RileyE
    Dec 28, 2012 at 20:08
24

With XCode 4.5+ and LLVM 4.1 there is no need to @synthesize, you will get a _prop1 to refer to.

- (void) setProp1:(id)aProp
{
    _prop1 = aProp;
}

Will work just fine.

9

You shouldn't use "self" inside the setter since that creates the recursive call.

Also, you should check to make sure you're not assigning the same object, retain the new object and release the old object before assignment.

And you should redefine the setter name, as suggested above:

@synthesize prop1 = prop1_; 

...

- (void) setProp1:(id)aProp
{
    if (prop1_ != aProp) {
        [aProp retain]; 
        [prop1_ release]; 
        prop1_ = aProp;
    }
}
2
  • you should only do this for a retain property, the OP's example is an assign property.
    – pancake
    Jun 30, 2014 at 23:00
  • The part that saved me was You shouldn't use "self" inside the setter since that creates the recursive call. Once I replaced self. with an underscore, the magic took place. Oct 27, 2015 at 0:12
5

Another alternative is to set the synthesized variable to another name like so:

@synthesize selectedQuestion = _selectedQuestion;

And then refer to it as _selectedQuestion. This prevents accidentally writing selectedQuestion when you meant self.selectedQuestion.

However, Apple recommend against using underscore. You can use another name, but @Sherm's method is the best, imho.

4

Simply @synthesize the desired property in your subclass, then you can use that as the name to access the property directly:

Main Class interface:

@interface AClass : NSObject

@property (nonatomic, assign) id<someProtocol> delegate;

@end

Subclass interface:

@interface BCLass : AClass
@end

Subclass implementation:

@implementation BCLass

@synthesize delegate = _delegate;

- (void)setDelegate:(id<someProtocol>)d{
    _delegate = d;
}

@end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.