Amazon.com Widgets dot operator
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 28, 2014, 06:30:35 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Newbie Corner
| | |-+ dot operator
Pages: [1] Go Down
Print
Author Topic: dot operator (Read 1858 times)
skronwith
Newbie
*
Posts: 17


Email




on: 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
Logged
seerex
Full Member
***
Posts: 177


Email




Reply #1 on: 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:
Code: (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:
Code: (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 Smiley
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW




Reply #2 on: August 28, 2011, 05:44:46 AM

Both
Code: (Objective-C)
[myRect setOrigin: myPoint]
and
Code: (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.
Logged
skronwith
Newbie
*
Posts: 17


Email




Reply #3 on: 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? 
Logged
seerex
Full Member
***
Posts: 177


Email




Reply #4 on: 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.
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #5 on: September 06, 2011, 10:28:38 AM

@skronwith: reread the discussion on pp. 129-130 to see if that helps.

Cheers,

Steve
Logged
skronwith
Newbie
*
Posts: 17


Email




Reply #6 on: 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? 
Logged
dharr19
Full Member
***
Posts: 175


Email




Reply #7 on: 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
Logged
skronwith
Newbie
*
Posts: 17


Email




Reply #8 on: 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.
Logged
BFB
Newbie
*
Posts: 6


Email




Reply #9 on: 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?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #10 on: 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
Logged
BFB
Newbie
*
Posts: 6


Email




Reply #11 on: September 14, 2011, 06:47:12 PM

Now I'm more confused. Why synthesize at all, if it's not required?
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #12 on: 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
Logged
BFB
Newbie
*
Posts: 6


Email




Reply #13 on: 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)
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.