Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan

Old Stuff => Newbie Corner => : skronwith August 27, 2011, 08:13:53 PM



: dot operator
: skronwith August 27, 2011, 08:13:53 PM
I've posted this in the chapter 8 forum but since I'm a newbie I figured I'd try here too.  In chapter 8, example 8.5 there is the line 

myRect.origin = myPoint;

Could someone please explain how exactly the compiler deals with this and specifically what are the steps that are taken that get us to calling the  setOrigin method here. Please don't leave anything out if you think it's too simple because at the moment, nothing is too simple for me.  Thanks.

Steve


: Re: dot operator
: seerex August 28, 2011, 05:04:35 AM
When calling myRect.origin = myPoint, you are using your synthesized setter / getter methods, so the previous line of code is equal to:
: (Objective-C)
[myRect setOrigin: myPoint];

The declaration of the origin instance variable, is declared to hold a reference / pointer to an XYPoint Object, with this line in the .h file of Rectangle:
: (Objective-C)
XYPoint* origin;

When you do the message call, you are taking an instance of the Rectangle class you created - myRect - and passes it the argument myPoint (which is a variable that holds a reference / pointer to an XYPoint object in memory) - this now means, that it passes it's reference / pointer, and puts it into origin. So now, both the instance variable origin and myPoint, will be pointing to the same object in memory.

Hope it clears it up, if not, let me know :)


: Re: dot operator
: fujilla August 28, 2011, 05:44:46 AM
Both
: (Objective-C)
[myRect setOrigin: myPoint]
and
: (Objective-C)
myRect.origin = myPoint
are using the synthesised setter/getter methods, it's just that doing so allows you to use dot syntax should you wish to.


: Re: dot operator
: skronwith August 31, 2011, 08:20:29 PM
Thanks everyone but I don't get how you can say we're using synthesized setter and getter methods.  The only variables that are synthesized in the program are x,y and width and height so my understanding is the synthesized setter methods would be SetX, SetY, SetWidth and SetHeight.  The programmer himself created SetOrigin.  Does this mean that the compiler, if it sees a method that starts with Set and then tacks on a instance variable with the first letter capitalized will always assume we're setting up a setter method? 


: Re: dot operator
: seerex September 04, 2011, 11:57:57 AM
Can you maybe post the complete program code? Coz i'm note sure what you are confused about.


: Re: dot operator
: skochan September 06, 2011, 10:28:38 AM
@skronwith: reread the discussion on pp. 129-130 to see if that helps.

Cheers,

Steve


: Re: dot operator
: skronwith September 08, 2011, 08:29:02 AM
Steve,

Is there any way to translate the pages i129-130 n the book to locations in the e-book? 


: Re: dot operator
: dharr19 September 08, 2011, 08:39:07 AM
Hi,

I you are using Kindle software the number will be at the bottom of the window. If you are using Adobe Reader and you click on thumbnails you can scroll down to the page you are looking for.

Hope this helps
David


: Re: dot operator
: skronwith September 08, 2011, 07:03:01 PM
I'm using the Kindle app on the ipad and it only gives location numbers, not page numbers.


: Re: dot operator
: BFB September 13, 2011, 10:30:09 PM
I am confused about the use of the dot syntax in this example too. I thought you could only use the dot syntax if you first synthesized the instance variables using the @property and @synthesize directives in the interface and implementation files, respectively. But in example 8.5, he uses the command myRect.origin = myPoint; and never "synthesized" origin. In order to use that command, shouldn't you first have to synthesize the rectangle object instance variable, origin?


: Re: dot operator
: skochan September 13, 2011, 11:26:07 PM
You do not have to synthesize the methods to use the dot operator.   The dot operator is just an alternate syntax for executing a method according to the identity described on pp 129-130.    The fact that the dot operator and the @property/@synthesize directives were all added to Objective-C at the same time (with version 2.0) may lead some to think that they have to be used together.  In fact, they are often used together, but it's not a requirement.

Cheers,

Steve


: Re: dot operator
: BFB September 14, 2011, 06:47:12 PM
Now I'm more confused. Why synthesize at all, if it's not required?


: Re: dot operator
: skochan September 14, 2011, 06:54:01 PM
You synthesize so you don't have to write the code yourself.  You synthesize so that the system can do "systemy-things" if it wants in your setters and getters (such as provide exclusive access to a property by using the atomic attribute, which makes the code thread-safe).  You synthesize to give external access to a property that can translate to a different name for the corresponding instance variable, which forces access through the propert---this is a trend Apple continues to promote with its template files.

Cheers,

Steve


: Re: dot operator
: BFB September 14, 2011, 07:26:45 PM
Let me see if I understand here. When you synthesize instance variables, you're basically turning them into methods, no? What you're saying is that custom methods that you define for the object are already "messageable" using the dot syntax. It's just you need to synthesize the instance variables to use the dot syntax as if they were, in fact, methods. Can you only use the dot syntax with methods that set or return a value? (eg. myFraction.print)


Sorry, the copyright must be in the template.
Please notify this forum's administrator that this site is missing the copyright message for SMF so they can rectify the situation. Display of copyright is a legal requirement. For more information on this please visit the Simple Machines website.