0

This question is similar to this SO question. But since it does not address my issue completely, I am asking another one.

Suppose I have a CGRect property myRect.

I see that self.myRect.origin = CGPointMake(x,y); does not work (not assignable) but _myRect.origin = CGPointMake(x,y); works.

I think it is because self.myRect is converted to a call to the accessor and so it does not get translated to what we want [self setMyRect:CGRect(x,y,width,size)]. How does the compiler translate the self.myRect.origin = CGPointMake(x,y); statement?

Community
  • 1
  • 1
Smart Home
  • 801
  • 7
  • 26
  • 1
    The answers in the question you link are answers to your question as well. – rmaddy Nov 07 '16 at 18:33
  • Can you explain how those answers don't address your question completely? You ask "How does the compiler translate the self.myRect.origin = CGPointMake(x,y); statement?" From the accepted answer: "The "myView.frame" part equals [myView getFrame] and you get a copied CGRect frame (a C struct) The "myView.frame.origin" gives you a CGPoint origin (also a struct) of the copied CGRect The "myView.frame.origin.x = 25.0" gives you a CGFloat x of the origin and now you want to assign something to it and here comes the problem..." – jscs Nov 07 '16 at 18:48
  • It's not clear what information that you need is missing. – jscs Nov 07 '16 at 18:54
  • @JoshCaswell, In my case, I am just using a generic CGRect (i.e not accessing the frame property on a view). In this case, won't self.myRect just be a getter for the property. I.e I don't understand why this returns a copied version of the property (i.e copy of CGRect struct)? – Smart Home Nov 08 '16 at 00:44
  • `UIView`'s `frame` is just a `CGRect` property, too. It returns a copy because that's just how structs work. There's no difference unless you have some bizarre custom implementation of your property. – jscs Nov 08 '16 at 00:47
  • Thanks, @JoshCaswell. Is this "return a copy" specific to structs or are there situations which behave like this. Just to confirm, if the property is an objective-C class, then when accessing through getter, you are getting pointer to the actual object and not a copy, correct? – Smart Home Nov 08 '16 at 01:00
  • That's right, for an object property you get a pointer to the same object. (For comparison, you can also work with pointers to `struct`s, but that requires explicit handling.) Any non-object type is giving you a copy of the value, it's just less noticeable with, e.g., an `int`, because you don't expect to set just a part of the `int` like you do with a `struct`. – jscs Nov 08 '16 at 01:07

0 Answers0