0

If a class has a custom setter for a property:

@interface OuterClass : NSObject
@property InnerClass *obj;
-(void)setObj:(InnerClass *)obj;

and InnerClass itself has a property:

@property NSString *text;

and I then do: obj.text = @"Hello"; will that activate the OuterClass object's setObj: method?

jscs
  • 63,694
  • 13
  • 151
  • 195
Joel Fischer
  • 6,521
  • 5
  • 35
  • 46
  • 1
    @AndrewMadsen not duplicate – Justin Meiners Mar 12 '13 at 22:26
  • @JustinMeiners defend your position. I see your answer and the other one in that linked dupe look remarkably similar in content. – CodaFi Mar 12 '13 at 23:10
  • 1
    @CodaFi the question in the link is identical to my first answer (section below BEFORE EDIT) which is asking basically asking if an property does setter and getter. This question is asking if an object with a member gets notified when a members property changes. Different questions about the same functionality in different contexts. – Justin Meiners Mar 12 '13 at 23:41

2 Answers2

4

No, setObj: will not be called because you are not setting the property. You are getting the property and then setting one of the result's properties. This is equivalent to [[something obj] setText:@"Hello"].

Chuck
  • 234,037
  • 30
  • 302
  • 389
3

Sorry I misunderstood the context of your original question.

Calling someObject.obj.text = @"Hello" will not call -setObj on the outer class.

@properties generate simple setters and getters and do not observe changes made to members of ivars.

It will however call -obj on someObject.

Think of it like this. Given someObject.obj.text =, the someObject.obj section uses the getter of someObject to return obj. The obj.text = part then sends -setText to the result of the obj getter.

BEFORE EDIT:

Yes, an @property automatically generates a setter and getter for the ivar.

Setting obj.text = @"hello" calls the -setText: method.

and accessing the property x = obj.text calls the -text method.

Make sure if you override a setter or getter that the method matches the property exactly.

Justin Meiners
  • 10,754
  • 6
  • 50
  • 92
  • Yeah, and I'm using the generated setter (customized). I'm wondering if setting the inner object property will activate the outer object's setObj: method because it's property was modified. I'm aware I'm not explaining myself super well. – Joel Fischer Mar 12 '13 at 21:59